struts2中自定义拦截器
来源:互联网 发布:php源码分享 编辑:程序博客网 时间:2024/06/04 17:46
今天struts实验课 要求struts自定义拦截器,使用户名不是以r结尾的用户不不得访问/sec命名空间下的Action
很简单 从jsp页面中获取输入的用户名,在拦截器中判断下即可。
拦截器的实现类
“`
package interceptor;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckQuanxian extends AbstractInterceptor {
@Overridepublic String intercept(ActionInvocation invocation) throws Exception {
// ActionContext ctx =invocation.getInvocationContext();// 获得Action上下文
// Map session = ctx.getSession(); // 获取session
// String user = (String) session.get(“username”);
HttpServletRequest request = ServletActionContext.getRequest();
String user = request.getParameter(“username”);
if (user.endsWith(“r”)) {
return invocation.invoke();
}
((ActionSupport) invocation.getAction()).addActionMessage(“用户名不是以r结尾,无权访问”);
return “login”;
}
}
但在拦截器的实现类中获取uesrname时遇到了麻烦
用invocation.getInvocationContext().getSession().get(“username”); 获取不到username的值。原因是根本就没向session里面存username这个值
解决办法
可用ServletActionContext访问servlet API用getParameter()获取
配置文件
注意配置文件有一定的是顺序的
result-types
interceptors
default-interceptor-ref
default-action-ref
default-class-ref
global-results
global-exception-mappings
action*(就是所有的action放到最后)
- Struts2中自定义拦截器
- struts2 中自定义拦截器
- struts2中自定义拦截器
- Struts2中自定义拦截器
- Struts2中自定义拦截器
- struts2中拦截器的全局拦截和自定义拦截
- struts2中自定义拦截器的配置
- struts2中自定义拦截器详解
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- maven常用命令
- iOS中造成dealloc不调用的原因
- C++实验三项目一
- 3.1.15 结构体的交互引用
- 1010. Radix (25)
- struts2中自定义拦截器
- 揭开“流量劫持”的神秘面纱
- Edit Distance(两字符串经过最少操作匹配)
- 3.1.16 函数的副作用
- AngularJS 1.x学习<2>
- Android中Application类用法
- 把 HTML 实体转换为字符:html_entity_decode() 函数
- 第四次上机作业
- poj 3041 Asteroids