日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > 利用SpringMVC攔截器控制Controller返回值

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

來源:程序員人生   發布時間:2014-10-10 08:00:00 閱讀次數:2719次

    背景:需求是在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注解,因為這個注解可以讓你知道你的方法要返回一個什么樣的結果。

敏捷是一條很長的路,摸索著前進著!!






生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲欧美自拍视频 | 国产精品一区久久 | 中国毛片a | 久久99精品久久久久久 | 日韩欧美自拍偷拍 | 久久亚洲视频 | 国产成人精品免费 | 国产日韩欧美一区二区 | 婷婷成人精品视频在线观看 | 国产精品久久久久久久久久久久 | 国产精品久久久久久久av大片 | 99精品国产热久久91蜜凸 | 亚洲网站在线看 | 欧美福利在线观看 | 男女国产视频 | 欧美一二| 国产精品免费一区二区三区在线观看 | 日韩欧美一区二区在线视频 | 亚洲综合视频在线 | 黄色激情网址 | 毛片日韩| 不卡中文| 久久久久久久久免费 | 日韩一二 | 国产精品视频区 | 精品一区二区三区四区五区 | 九九九久久国产免费 | 久久久精品久久久久 | 91嫩草视频在线 | 亚洲专区欧美专区 | 欧美日韩福利视频 | 免费毛片a线观看 | 亚洲免费大全 | 国产麻豆乱码精品一区二区三区 | 国产成人精品一区二区三区在线 | 国产在线一区二区三区四区 | 精品日韩欧美 | 91精品国产综合久久久久久久久 | 亚洲欧美激情视频 | 日韩视频区 | 尤物国产|