背景:需求是在Controller中方法没有实现时,返回模拟结果。主要用于项目初期前台跟后台的交互,Web项目就是在前台发出请求然后后台响应并返回结果。本示例利用拦截器和注解实现跳过执行方法直接返回定义结构的功能。
通过定义一个StringResult注解,在访问方法的时候返回StringResult中的内容。通过Debug注解来定义方法是否要返回StringResult中的内容。
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(); 
} 
 | 
定义好注解之后写拦截器类,拦截器需要实现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中的内容返回给前台 
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!"; 
} 
} 
 | 
此方法可用以在控制器中的方法没有写好的时候进行前台功能的测试,思路大概如此,更加强大的功能需要各位大神们开发。这个只是我的突发奇想,并没有实际在项目中试过。如果有人在项目中试了请告诉我效果,谢谢。
如果有人用了,建议保留StringResult注解,因为这个注解可以让你知道你的方法要返回一个什么样的结果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/jaune161/article/details/39639037
相关文章
             猜你喜欢
        
        - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 - 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
 - ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
 - 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
 
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-05-29 29
 - 
            2025-05-29 79
 - 
            2025-05-29 16
 - 
            2025-05-25 100
 - 
            2025-05-27 40
 
		热门评论
	
	
        
    		
            	
        
        
        