Struts 2 自定义拦截器创建与使用
来源:互联网 发布:java graphics2d 旋转 编辑:程序博客网 时间:2024/05/22 07:48
Strus2 提供了Interceptor 接口,通过该接口可以很容易的实现一个拦截器类。开发者只需要直接或间接实现Interceptor接口
public interface Interception extends Serializable{
void destroy();
void init();
String intercept(ActionInvocation invccation) throws Exception;
}
init(); 由拦截器之前调用,主要用于初始化系统资源。
destroy();与init()相反,用于拦截器之后执行销毁资源。
intercept();拦截器的核心方法,实现具体的拦截操作,返回字符串作为逻辑视图,与Action一样,如果拦截器能够成功调用Action,则Action 种的execute()方法返回一个字符串类型值,将器作为逻辑视图返回,反之返回一个开发者自定义的一个逻辑视图。
用户登陆拦截器:
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class interfac extends AbstractInterceptor {
public String intercept(ActionInvocation ai) throws Exception {
Map m = (Map) ai.getInvocationContext().getParameters();
String[] username = (String[]) m.get("username");
String u = username[0];
if (u != null && u.length() > 0) {
return ai.invoke();
} else {
ActionContext ac = ai.getInvocationContext();
ac.put("sorry", "您还没有登陆");
return Action.LOGIN;
}
}
}
struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<includefile="struts-default.xml"/>
<packagename="login"extends="struts-default"namespace="/login">
<interceptors>
<interceptorname="login"class="com.poba.model.interfac"/>
<interceptorname="public"class="com.poba.model.Myintercept"/>
<interceptor-stackname="loginchect">
<interceptor-refname="login"/>
<interceptor-refname="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-refname="loginchect"/>
<actionname="login"class="com.poba.model.LoginAction"method="checkLogin">
<resultname="success"> /index.jsp</result>
<resultname="login">/login.jsp</result>
<resultname="input">/login.jsp</result>
</action>
</package>
</struts>
- Struts 2 自定义拦截器创建与使用
- 自定义struts过滤器与拦截器实现
- 在Struts 2中自定义拦截器的使用
- struts自定义拦截器
- Struts自定义拦截器
- Struts-自定义拦截器
- struts自定义拦截器学习
- struts自定义拦截器解读
- struts拦截器使用
- Struts拦截器使用
- struts拦截器使用
- struts 拦截器 使用
- Struts 2读书笔记-----拦截器之拦截器的使用
- 框架技术--struts2拦截器与自定义拦截器使用
- Struts 2拦截器
- Struts 2拦截器
- struts 2 拦截器
- Struts 2 拦截器
- cocos2d坐标位置总结
- oracle 触发器
- 无损检测
- UINavigationBar 设置背景图片
- Dos下常用命令和快捷键
- Struts 2 自定义拦截器创建与使用
- 构造函数和析构函数中的虚函数
- LINQ to Objects(2)
- div+CSS瀏覽器兼容問題整理(IE6.0、IE7.0 ,ie8 , FireFox...)
- jQuery的Select操作集合
- 第九周C++作业报告六 (第八周四)
- 如何减小与“大牛”的差距
- 结构张量扩散
- 一名保安捡到内装11多万元现金的名牌手袋