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>
    
  • 相关阅读:
    2 行为型模式之
    1 行为型模式之
    WinSCP无法连接 ubuntu 的解决方法
    command 'x86_64-linux-gnu-gcc' failed with exit status 1错误及解决方案
    Ubuntu 16.04 LTS 安装 Nginx/PHP 5.6/MySQL 5.7 (LNMP) 与Laravel
    CentOS 7 安装、配置、使用 PostgreSQL 9.5及PostGIS2.2
    R实战之热点图(HeatMap)
    Windows下Eclipse连接hadoop
    Ubuntu下eclipse开发hadoop应用程序环境配置
    Hadoop集群环境搭建
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9464216.html
Copyright © 2011-2022 走看看