zoukankan      html  css  js  c++  java
  • Struts2之拦截器

    1.第一种方式

    拦截器类书写

    public class UserInterceptor extends AbstractInterceptor  {
    
    	@Override
    	public String intercept(ActionInvocation invocation) throws Exception {
    		
    		System.out.println("我在action之前执行!");
    		
    		System.out.println("拦截时间:"+new Date());
    		
    		//执行的是Action中的方法
    		String result = invocation.invoke();
    		
    		System.out.println("我在action之后执行!");
    		
    		return result;
    		
    	}
    
    }
    

    struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	<interceptors>
    		<interceptor name="userInterceptor" class="cn.jiangwenwen.interceptor.UserInterceptor"></interceptor>
    	</interceptors>
    		
    	<action name="login" class="cn.jiangwenwen.action.UserAction" method="login">
    		<result>/test.jsp</result>
    		<interceptor-ref name="userInterceptor"></interceptor-ref>
    		<!-- 当使用自定义拦截器,默认拦截器将会失效,所以需要添加默认拦截器 -->
    		<interceptor-ref name="defaultStack"></interceptor-ref>	
    	</action>
    </package>
    </struts>
    

    2.第二种方式

    拦截器类书写

    public class UserInterceptor extends MethodFilterInterceptor{
    
    	@Override
    	protected String doIntercept(ActionInvocation invocation) throws Exception {
    		
    		 System.out.println("我在action之前执行!");
    	        
    	        System.out.println("拦截时间:"+new Date());
    	        
    	        //执行的是Action中的方法
    	        String result = invocation.invoke();
    	        
    	        System.out.println("我在action之后执行!");
    	        
    	        return result;
    		
    	}
    
    }
    

    struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	<interceptors>
    		<interceptor name="userInterceptor" class="cn.jiangwenwen.interceptor.UserInterceptor"></interceptor>
    	</interceptors>
    				
    	<action name="login" class="cn.jiangwenwen.action.UserAction" method="login">
    		<result>/test.jsp</result>
    		<interceptor-ref name="userInterceptor">
    			<!-- 代表是否拦截某一个方法 -->
    			<param name="excludeMethods">login</param>
    		</interceptor-ref>
    		<!-- 当使用自定义拦截器,默认拦截器将会失效,所以需要添加默认拦截器 -->
    		<interceptor-ref name="defaultStack"></interceptor-ref>	
    	</action>
    </package>
    </struts>
    
  • 相关阅读:
    JSON数据的中文乱码问题
    使用json-lib进行Java和JSON之间的转换
    struts2 struts.xml配置文件详解
    php位运算的应用(转)
    oracle数据库导入导出的dmp(转)
    Java多线程-工具篇-BlockingQueue(转)
    javaweb之Filter详解(转)
    MYSQL写入数据时报错ERROR 1366 (HY000): Incorrect string value: 'xE8x8BxB1xE5xAFxB8...' for c 插入中文不能插入
    插入中文错误ERROR 1406 (22001): Data too long for column 'name' at row 1
    SQL语句处理一些修改、新增、删除、修改属性操作(MySql)
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9464216.html
Copyright © 2011-2022 走看看