struts2拦截器学习

      当需要使用struts里定义的拦截器时,需要在struts.xml中引入“<include file="struts-default.xml" />”,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。
   简单例子:配置一个名为TimerInterceptorAction的action

public class TimerInterceptorAction extends ActionSupport{
	public String excute(){
		 try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return SUCCESS;
	}
}

         在struts.xml中配置相应的action

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-default.xml" />
<package name="TimeTest" extends="struts-default">
		<action name="Timer" class="com.opensymphony.xwork2.TimerInterceptorAction">
			<interceptor-ref name="timer" />
			<result>Time.jsp</result>
		</action>
	</package>
</struts>   

 在Time.jsp上随意写东西就可以测试timer拦截器。

2.自定义拦截器

    新建LoginIntercept.java

 

public class LoginIntercept extends AbstractInterceptor{
	public String intercept(ActionInvocation invocation) throws Exception{
		Object admin = ActionContext.getContext().getSession().get("admin");
		System.out.println("自定义拦截器被执行了,恭喜");
		System.out.println(admin);
		if(!admin.equals("admin")){
			return Action.LOGIN;
		}
		else{
			System.out.println("验证通过");
			return invocation.invoke();
		}
		
	}
}

      UserLoginAction.java

 

public class UserLoginAction extends ActionSupport{
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String userLogin() throws Exception{
		String msg = "";
		System.out.println(user.getName() + ":" + user.getPassword());
		if(user.getName().equals("admin") && user.getName() != null){
			if(user.getPassword().equals("admin") && user.getPassword() != null){
				ActionContext.getContext().getSession().put("user", user);
				
				return SUCCESS;
			}else{
				this.addFieldError(msg, "你输入的密码有误,请重新输入");
				return LOGIN;
			}
		}else{
			this.addFieldError(msg, "用户名输入不正确,请重新输入");
			return LOGIN;
		}
	}
}

 User.java

 

public class User {
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

 配置struts.xml文件

 

<!--登录拦截器验证  -->
	<package name="User" extends="struts-default">
		<!--设置自己定义的拦截器  -->
		<interceptors>
			<interceptor name="loginIntercept" class="com.intercept.LoginIntercept" >
			</interceptor>
			<interceptor-stack name="loginInterceptStack">
				<interceptor-ref name="loginIntercept" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		
		<!--设置全局login  -->
		<global-results>
			<result name="login" type="redirect">login.jsp</result>
		</global-results>
		
		<!--设置UserLoginAction  -->
		<action name="userloginAction" class="com.action.UserLoginAction" method="userLogin">
			<result name="success">loginSuc.jsp</result>
			<result name="login">loginTest.jsp</result>
			<interceptor-ref name="loginInterceptStack" />
		</action>
	</package>

 loginTest.jsp

 

<s:fielderror value="msg" />
    <s:form action="userloginAction" method="post">
    	<s:textfield name="user.name" label="username" /><br />
    	<s:textfield name="user.password" label="password" /><br />
    	<s:submit value="Submit" /><s:reset value="Reset" />
    </s:form>

     自定义拦截器时,可以struts2给我们俩种方式。一个是接口intercept ,另一个是AbstractInterceptor抽象类。这些方式中都有一个 intercept(ActionInvocation invocation)方法,这是实现的核心。

 

你可能感兴趣的:(struts)