您的位置:首页 > 编程语言 > Java开发

struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例

2016-06-12 14:52 393 查看
1.用户登陆,随后表单提交

<body>
<s:fielderror />
<s:form action="login">
<s:textfield name="name" label="用户名" />
<s:password name="password" label="密码" />
<s:submit value="登录"></s:submit>
</s:form>

</body>2.提交后,被struts2的核心过滤器拦截

3.自定义拦截器,两种方法:extends AbstractInterceptor       implements Interceptor   用来进行登陆的权限控制功能的实现
<pre name="code" class="java">
//import com.opensymphony.xwork2.ActionInvocation;
//import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
//
//public class MyTimerInterceptor extends AbstractInterceptor {
//
//	@Override
//	public String intercept(ActionInvocation invocation) throws Exception {
//		// TODO Auto-generated method stub
//		long start=System.currentTimeMillis();
//		String result=invocation.invoke();
//		System.out.println(result);
//		long end=System.currentTimeMillis();
//		System.out.println("��actionִ��ʱ��Ϊ��"+(end-start));
//		return result;
//	}
//
//}




import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginedInterceptoer implements Interceptor {

public void destroy() {
// TODO Auto-generated method stub

}

public void init() {
// TODO Auto-generated method stub

}

public String intercept(ActionInvocation invocation) throws Exception {

// TODO Auto-generated method stub将登陆的用户名存放session集合中,

Map map = invocation.getInvocationContext().getSession();
Object obj = map.get("user");
if (obj == null) {
return "input";
} else {
return invocation.invoke();
}

}

}

4.表单提交后,被过滤器拦截,执行action类。

<pre name="code" class="java">public class LoginAction extends ActionSupport {
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;
}

public String login(){
if("sa".equals(name)&&"123".equals(password)){
Map map=ActionContext.getContext().getSession();
map.put("user", name);
return SUCCESS;
}else{
return LOGIN;
}
}
}



5.配置文件实现过滤器的组合

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" extends="struts-default" namespace="/">
<!-- 配置 拦截器 -->
<interceptors>
<interceptor name="mytime" class="com.yh.inter.MyTimerInterceptor"></interceptor>
<interceptor name="LoginedInter" class="com.yh.inter.LoginedInterceptoer"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="mytime"></interceptor-ref>
<interceptor-ref name="LoginedInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="index" >
<result>/index.jsp</result>
<!-- 引用 拦截器 -->
<interceptor-ref name="myStack"></interceptor-ref>
<result name="input">/login.jsp</result>
</action>

<action name="login" class="com.yh.action.LoginAction" method="login">
<result type="redirectAction">index</result>
<result name="login">/login.jsp</result>
</action>

</package>
</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0