欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解利用SpringMVC攔截器控制Controller返回值

 更新時間:2017年01月19日 14:47:55   作者:王成委  
這篇文章主要介紹了詳解利用SpringMVC攔截器控制Controller返回值,通過定義一個StringResult注解,在訪問方法的時候返回StringResult中的內容,有興趣的可以了解一下。

背景:需求是在Controller中方法沒有實現(xiàn)時,返回模擬結果。主要用于項目初期前臺跟后臺的交互,Web項目就是在前臺發(fā)出請求然后后臺響應并返回結果。本示例利用攔截器和注解實現(xiàn)跳過執(zhí)行方法直接返回定義結構的功能。

通過定義一個StringResult注解,在訪問方法的時候返回StringResult中的內容。通過Debug注解來定義方法是否要返回StringResult中的內容。

Debug默認為TRUE

package com.tiamaes.dep.annotation; 
 
import java.lang.annotation.Documented; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
 
@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface Debug { 
  boolean value() default true; 
} 
package com.tiamaes.dep.annotation; 
 
import java.lang.annotation.Documented; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
 
@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface StringResult { 
  String value(); 
} 

定義好注解之后寫攔截器類,攔截器需要實現(xiàn)HandlerInterceptor

package com.tiamaes.dep.interceptor; 
 
import java.io.PrintWriter; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.springframework.web.method.HandlerMethod; 
import org.springframework.web.servlet.HandlerInterceptor; 
import org.springframework.web.servlet.ModelAndView; 
 
import com.tiamaes.dep.annotation.Debug; 
import com.tiamaes.dep.annotation.StringResult; 
 
public class DebugInterceprot implements HandlerInterceptor { 
  private boolean debug = true; 
   
  public boolean preHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler) throws Exception { 
    //首先判斷是否是Debug模式(全局),如果否則使攔截器失效 
    if(!this.debug) return true; 
     
    if(handler instanceof HandlerMethod){ 
      HandlerMethod method = (HandlerMethod)handler; 
      Debug isDebug = method.getMethodAnnotation(Debug.class); 
      StringResult stringResult = method.getMethodAnnotation(StringResult.class); 
      //如果沒有@StringResult注解則跳過攔截 
      //判斷方法上注解的Debug值,如果否則不攔截 
      if(stringResult==null||(isDebug !=null && isDebug.value() == false)){ 
        return true; 
      }else{ 
        //攔截方法,并將stringResult中的內容返回給前臺 
        PrintWriter out = response.getWriter(); 
        out.print(stringResult.value()); 
      } 
    } 
     
    return false; 
  } 
   
  public void postHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler, 
      ModelAndView modelAndView) throws Exception { 
    // TODO Auto-generated method stub 
 
  } 
 
  public void afterCompletion(HttpServletRequest request, 
      HttpServletResponse response, Object handler, Exception ex) 
      throws Exception { 
    // TODO Auto-generated method stub 
 
  } 
 
  public boolean isDebug() { 
    return debug; 
  } 
 
  public void setDebug(boolean debug) { 
    this.debug = debug; 
  } 
   
   
 
} 

XML配置

<mvc:interceptors> 
  <mvc:interceptor> 
    <mvc:mapping path="/**"/> 
    <bean class="com.tiamaes.dep.interceptor.DebugInterceprot"> 
      <property name="debug" value="true"/> 
    </bean> 
  </mvc:interceptor> 
</mvc:interceptors> 

Controller中的寫法

package com.tiamaes.dep.system.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
import com.tiamaes.dep.annotation.Debug; 
import com.tiamaes.dep.annotation.StringResult; 
 
@Controller 
 
@RequestMapping("/test") 
public class AspectTestController { 
 
  @RequestMapping("/1") 
  @ResponseBody 
  //@Debug(false) 
  @StringResult("Interceptor") 
  public String test1(){ 
     
    return "The controller request!"; 
  } 
} 

此方法可用以在控制器中的方法沒有寫好的時候進行前臺功能的測試,思路大概如此,更加強大的功能需要各位大神們開發(fā)。這個只是我的突發(fā)奇想,并沒有實際在項目中試過。如果有人在項目中試了請告訴我效果,謝謝。

如果有人用了,建議保留StringResult注解,因為這個注解可以讓你知道你的方法要返回一個什么樣的結果。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • java中多線程的超詳細介紹

    java中多線程的超詳細介紹

    這篇文章主要給大家介紹了關于java中多線程的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • Java動態(tài)規(guī)劃之編輯距離問題示例代碼

    Java動態(tài)規(guī)劃之編輯距離問題示例代碼

    這篇文章主要介紹了Java動態(tài)規(guī)劃之編輯距離問題示例代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 如何使用Sentry 監(jiān)控你的Spring Boot應用

    如何使用Sentry 監(jiān)控你的Spring Boot應用

    這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • MyBatis無縫對接Spring的方法

    MyBatis無縫對接Spring的方法

    Spring框架與MyBatis框架是Java互聯(lián)網技術的主流框架。那么mybatis如何無縫對接spring呢?下面通過本文給大家介紹,需要的的朋友參考下吧
    2017-09-09
  • Ajax實現(xiàn)搜索引擎自動補全功能

    Ajax實現(xiàn)搜索引擎自動補全功能

    本文主要介紹了Ajax實現(xiàn)搜索引擎自動補全功能的實例解析。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • 使用java寫的矩陣乘法實例(Strassen算法)

    使用java寫的矩陣乘法實例(Strassen算法)

    這篇文章主要給大家介紹了關于如何使用java寫的矩陣乘法(Strassen算法)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Maven如何打入依賴中指定的部分jar包

    Maven如何打入依賴中指定的部分jar包

    當項目運行的環(huán)境里已經有一個jar包是pom文件依賴其他項目的jar包,所以最后得到的項目jar包中還需要打入其他項目的最新代碼,接下來通過本文給大家介紹Maven打入依賴jar包的操作工程,需要的朋友參考下吧
    2021-06-06
  • SpringCloud使用Zookeeper作為配置中心的示例

    SpringCloud使用Zookeeper作為配置中心的示例

    這篇文章主要介紹了SpringCloud使用Zookeeper作為配置中心的示例,幫助大家更好的理解和學習使用SpringCloud,感興趣的朋友可以了解下
    2021-04-04
  • Quartz作業(yè)調度基本使用詳解

    Quartz作業(yè)調度基本使用詳解

    這篇文章主要為大家介紹了Quartz作業(yè)調度基本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用

    淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用

    這篇文章主要介紹了淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論