struts2学习笔记-----拦截器
来源:互联网 发布:2016淘宝客推广 编辑:程序博客网 时间:2024/05/17 16:43
配置拦截器语法格式
<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{@Overridepublic String intercept(ActionInvocation ai) throws Exception {// TODO Auto-generated method stubObject 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学习笔记
- struts2 学习笔记拦截器
- struts2学习笔记-----拦截器
- struts2系列学习笔记(11)---------------拦截器
- struts2学习笔记之拦截器(Interceptor)
- Struts2拦截器Interceptor学习笔记
- Struts2学习笔记之自定义拦截器
- struts2学习笔记拦截器实现原理
- Struts2学习笔记之自定义拦截器
- struts2学习笔记(九)拦截器
- struts2 拦截器学习笔记01
- struts2学习笔记之拦截器(Interceptor)
- Struts2学习笔记五(拦截器)
- Struts2学习笔记---拦截器实例两个
- Struts2.0学习笔记---拦截器拦截方法
- Struts2学习笔记11:Struts2的拦截器
- Struts2学习笔记11:Struts2的拦截器
- Struts2学习笔记12:Struts2的拦截器【续】
- Java调用C和C++函数时的JNI使用区别
- 我的U盘终于中招啦:U盘快捷方式病毒
- 最大流问题
- 使用GCD
- VC程序中打开一个另一个程序并关闭
- struts2学习笔记-----拦截器
- 混合高斯模型
- 视图类动态创建视图窗口
- 从最大似然到EM算法浅解
- surfer 8 scripter 学习笔记(2)Application对象
- 如何把string的元素逆序
- Factorial of a number
- JavaSE 关于复杂对象生成过程与try-finally的理解
- u-boot移植记录