国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 13:13:07
字體:
來源:轉載
供稿:網友

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

通過定義一個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(); } 

定義好注解之后寫攔截器類,攔截器需要實現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!";   } } 

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秭归县| 泾源县| 山丹县| 荥经县| 苗栗市| 盐源县| 徐闻县| 汶川县| 凭祥市| 武隆县| 班玛县| 瓦房店市| 尚义县| 庆阳市| 黄浦区| 冕宁县| 琼结县| 西乌珠穆沁旗| 砀山县| 张家界市| 平顶山市| 泸溪县| 滕州市| 宁化县| 安宁市| 定南县| 磐安县| 鄱阳县| 基隆市| 青冈县| 镇巴县| 南陵县| 莱芜市| 项城市| 吉安县| 望奎县| 连山| 广元市| 永川市| 满洲里市| 阿拉善右旗|