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.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>
<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>
相关文章推荐
- Struts - 标签库
- struts1.0于struts2.0的比较
- Struts2中关于namespace的学习心得
- Struts2----自定义action的学习心得
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- struts2
- SSH学习笔记---struts2 spring集成时action的class的值
- ssh--问题记录
- 文章标题 使用ajaxFileupload+struts2完成文件的上传以及回显到jsp的链接地址
- struts 学习笔记
- Struts2 的基本配置
- struts2框架架构流程
- struts2.0拦截器(实现未登录的用户不能访问系统的页面)(转)
- Struts2 面试题分析
- Struts2+jQuery+JSON实现异步交互
- 软件开发疑惑解答
- ajax ssh 整合
- 关于struts2框架学习
- struts2 @Action给方法注解
- struts2学习笔记