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

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

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

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

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

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

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

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

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

Debug默認(rèn)為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();
}

定義好注解之后寫攔截器類,攔截器需要實(shí)現(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注解則跳過(guò)攔截
      //判斷方法上注解的Debug值,如果否則不攔截
      if(stringResult==null||(isDebug !=null && isDebug.value() == false)){
        return true;
      }else{
        //攔截方法,并將stringResult中的內(nèi)容返回給前臺(tá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!";
  }
}

此方法可用以在控制器中的方法沒有寫好的時(shí)候進(jìn)行前臺(tái)功能的測(cè)試,思路大概如此,更加強(qiáng)大的功能需要各位大神們開發(fā)。這個(gè)只是我的突發(fā)奇想,并沒有實(shí)際在項(xiàng)目中試過(guò)。如果有人在項(xiàng)目中試了請(qǐng)告訴我效果,謝謝。

如果有人用了,建議保留StringResult注解,因?yàn)檫@個(gè)注解可以讓你知道你的方法要返回一個(gè)什么樣的結(jié)果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 高清在线看 | 国产亚洲综合成人91精品 | 日本精品人妖shemale人妖 | 美女张开双腿让男人捅 | 亚洲 欧美 制服 校园 动漫 | 99精品热线在线观看免费视频 | 亚洲国产99999在线精品一区 | 久久精品无码一区二区日韩av | 91制片厂制作传媒免费版樱花 | 国产一区视频在线免费观看 | 日本公乱妇视频 | np小说h | 久久99热成人精品国产 | 欧美精品1区2区 | 91免费精品国自产拍在线可以看 | 日韩精品视频观看 | 美女脱了内裤打开腿让你桶爽 | 亚洲成人77777 | 国产日韩欧美色视频色在线观看 | 性xxxxxxx18老师 | 亚洲2023无矿砖码砖区 | 2019国内精品久久久久久 | 色欧美亚洲 | 桃色综合网 | 精品久久免费视频 | 好舒服好爽再快点视频 | 国产欧美日韩高清专区ho | 美女隐私部位视频网站 | 美女视频91| 四虎在线最新永久免费 | 秋霞理论最新三级理论最 | 女同志 videos | 国产偷啪| 欧美爽妇 | 久久视频在线视频观看精品15 | 热剧库| 欧美精品99久久久久久人 | 国产老村长足疗店对白 | 国产精品合集久久久久青苹果 | 第四色男人天堂 | 四虎影视紧急入口地址大全 |