struts2学习笔记-----拦截器
2013-05-11 10:59
459 查看
配置拦截器语法格式
<interceptor name="拦截器名字" class="对应的java类"/>
使用拦截器栈,为了能在多个动作中方便引用同一个或者几个拦截器,可以使用拦截器栈。
<interceptor >
<interceptor-stack name="拦截器栈名">
<interceptor-ref name="拦截器名字"/>
</interceptor-stack>
</interceptor>
当拦截器栈被配置一个action时,要想执行该action必须先执行拦截器栈里的所有拦截器
使用拦截器例子
在定义一个拦截器类的时候需要实现Interceptor接口,或者继承拦截器类AbstractInterceptor
例子:
1.创建jsp文件register.jsp
内如如下:
2.在src/action文件夹下创建Action类
3.创建逻辑视图
4.创建拦截器类
5.配置struts.xml
<interceptor name="拦截器名字" class="对应的java类"/>
使用拦截器栈,为了能在多个动作中方便引用同一个或者几个拦截器,可以使用拦截器栈。
<interceptor >
<interceptor-stack name="拦截器栈名">
<interceptor-ref name="拦截器名字"/>
</interceptor-stack>
</interceptor>
当拦截器栈被配置一个action时,要想执行该action必须先执行拦截器栈里的所有拦截器
使用拦截器例子
在定义一个拦截器类的时候需要实现Interceptor接口,或者继承拦截器类AbstractInterceptor
例子:
1.创建jsp文件register.jsp
内如如下:
<%@taglib prefix="s" uri="/struts-tags" %>
<center> <s:form action="reg" method="post"> <s:textfield name="userName" label="注册姓名" maxlength="20"></s:textfield> <s:password name="userPassword" label="注册密码"></s:password> <s:textarea name="userInfo" label="个人说明" cols="40" rows="5"></s:textarea> <s:submit value="提交"></s:submit> </s:form> </center>
2.在src/action文件夹下创建Action类
package action; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String userName; private String userPassword; private String userInfo; public String getUserInfo() { return this.userInfo; } public void setUserInfo(String userInfo) { this.userInfo=userInfo; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName=userName; } public String getUserPassword() { return this.userPassword; } public void setUserPassword(String userPassword) { this.userPassword=userPassword; } public String execute() { return "success"; } }
3.创建逻辑视图
<s:property value="userName"/><br> <s:property value="userPassword"/><br> <s:property value="userInfo"/>
4.创建拦截器类
package interceptor; import action.RegisterAction; import java.util.Map; import com.opensymphony.xwork2.*; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class RegisterInterceptor extends AbstractInterceptor{ @Override public String intercept(ActionInvocation ai) throws Exception { // TODO Auto-generated method stub Object object=ai.getAction(); if(object!=null) { if(object instanceof RegisterAction) { RegisterAction action=(RegisterAction)object; String userInfo=action.getUserInfo(); if(userInfo.contains("坏")) { userInfo=userInfo.replaceAll("坏", "*"); action.setUserInfo(userInfo); } return ai.invoke(); }else { return Action.LOGIN; } } else { return Action.LOGIN; } } }
5.配置struts.xml
<?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> <package name="default" extends="struts-default"> <action name="loginRegister" class="action.loginRegister" method="execute"> <result name="success">/addsuccess.jsp</result> </action> <action name="register" class="action.loginRegister" method="register"> <result name="success">/addsuccess.jsp</result> </action> </package> </struts>
相关文章推荐
- struts2系列学习笔记(11)---------------拦截器
- struts2第十三讲学习笔记,拦截器interceptor的详细讲解
- Struts2拦截器Interceptor学习笔记
- struts2学习笔记(8)——拦截器原理
- 【Struts2学习笔记(10)】自定义拦截器管理权限访问
- struts2学习笔记 -- day09 Struts2的拦截器
- Struts2学习笔记五 拦截器
- 黑马程序员---struts2学习笔记之六(自定义拦截器)
- Struts2学习笔记---拦截器实例两个
- Struts2 学习笔记——struts.xml文件之拦截器的配置
- Java学习笔记 Struts2拦截器实现权限登录
- Struts2学习笔记之自定义拦截器
- [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程
- Struts2学习笔记12:Struts2的拦截器【续】
- Struts2-学习笔记系列(14)-拦截器
- struts2 拦截器学习笔记01
- Struts2 学习笔记18 拦截器原理分析
- Struts2 入门学习笔记(二)——拦截器应用
- 【Struts2学习笔记-6--】Struts2之拦截器
- [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器