一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解利用SpringMVC攔截器控制Controller返回值

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

2020-07-29 14:49王成委 Java教程

這篇文章主要介紹了詳解利用SpringMVC攔截器控制Controller返回值,通過定義一個StringResult注解,在訪問方法的時候返回StringResult中的內(nèi)容,有興趣的可以了解一下。

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

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

Debug默認為TRUE

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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中的內(nèi)容返回給前臺
        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配置

?
1
2
3
4
5
6
7
8
<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中的寫法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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注解,因為這個注解可以讓你知道你的方法要返回一個什么樣的結(jié)果。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/jaune161/article/details/39639037

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性做久久久久久久久浪潮 | 亚洲欧美综合区自拍另类 | 午夜AV内射一区二区三区红桃视 | 91无毒不卡 | 天天中文| 亚洲品质自拍视频网站 | 久99视频精品免费观看福利 | 日本草草视频在线观看 | 国产亚洲精品第一综合另类 | 99热这里只有精品一区二区三区 | 王的视频视ivk | 国产精品欧美亚洲韩国日本 | 99国产高清久久久久久网站 | 精品精品国产自在久久高清 | chinesespanking网站 | 99久久999久久久综合精品涩 | 不卡日本 | av72成人| 91porny.首页| 91麻豆精品国产片在线观看 | 欧美成人免费观看bbb | 扒开腚眼子视频大全 | 国语视频高清在线观看 | 欧美亚洲第一页 | 处女私拍| 99久久99久久久精品齐齐鬼色 | 日韩一级片在线播放 | jizz农村野外jizz农民 | 韩国日本在线观看 | 二区三区不卡不卡视频 | 护士被多人调教到失禁h | 毛片免费观看的视频 | 国产精品色拉拉免费看 | 九九九久久久 | 欧美侏儒xxx | 岛国最新资源网站 | 国产精品亚洲一区二区 | 亚洲精品国产乱码AV在线观看 | 欧美成人在线影院 | 久热人人综合人人九九精品视频 | 欧美黑人一级 |