struts2--使用自定义拦截器
来源:互联网 发布:java json替换value值 编辑:程序博客网 时间:2024/05/20 10:56
引言:做一个后台管理系统,这个系统为企业内部系统,如果不登录时没有权限访问此系统的。这时,可以使用拦截器拦截非法登录的用户。
自定义拦截器需要实现接口com.opensymphony.xwork2.interceptor.Interceptor。当然struts2提供了接口的实现类AbstractInterceptor,继承之,重新方法即可。
拦截器代码:
package com.Interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class LoginInterceptor extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception { String user = (String) ActionContext.getContext().getSession().get("user"); if(null == user){ ActionContext actionContext = invocation.getInvocationContext();//也可ActionContext.getContext();获取 actionContext.put("tishi", "您还没有登录!请先登录!"); return "login"; } return invocation.invoke() ;//用户已经登录,进行后续操作} }
struts.xml配置:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="default" namespace = "/" extends = "struts-default"> <interceptors> <interceptor name="loginInterceptor" class="com.Interceptor.LoginInterceptor"/> <interceptor-stack name="myStack"> <interceptor-ref name="loginInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/> <action name="login" class = "com.action.LoginAction"> <result name = "success">/ok.jsp</result> <result name = "login">/index.jsp</result> <interceptor-ref name="defaultStack"/> </action> <action name="check" class = "com.action.CheckAction"> <result name = "success">/check_ok.jsp</result> <result name = "login">/index.jsp</result> </action> </package></struts>在Action中引用拦截器,则默认拦截器将不起作用;由于登录方法不需要拦截,故不需要拦截器。
LoginAction.java
package com.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class LoginAction extends ActionSupport{ private String username; private String password ;public String execute() { if(username.equals("admin") && password.equals("admin")){ ActionContext.getContext().getSession().put("user", username); return SUCCESS; }else { return LOGIN; }} public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }
登录界面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix = "s" uri = "/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <s:property value = "tishi"/> <form action="login" method = "post"> <s:textfield name = "username" label = "用户名"></s:textfield><br><br> <s:password name = "password" label = "密码"></s:password><br><br> <s:submit value = "登录"></s:submit> </form> </body></html>
通常我们会遇到一段时间后刷新网页需要重新登录,也是拦截器在起作用,此时需要在web.xml中配置session作用的时间。
<session-config> <session-timeout>1</session-timeout> </session-config>一分钟后失效
阅读全文
0 0
- struts2--使用自定义拦截器
- Struts2 自定义拦截器配置和使用
- struts2自定义拦截器的使用
- 十三、struts2使用自定义拦截器
- Struts2使用14 -- 拦截器4 -- 自定义拦截器
- 框架技术--struts2拦截器与自定义拦截器使用
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- WUST OJ 1609: Fcbruce swap the grid
- Android中的事件处理
- 2017.08.02工作日记
- c++指针与引用
- matlab常用工具
- struts2--使用自定义拦截器
- KMP
- Web 的攻击技术
- vlc实现多倍速播放http文件
- 1011. A+B和C (15)
- Another Graph Game
- netty(二) 伪异步I/O编程
- 实战css3画个机器猫后自己的得到
- Android 获取当前时间(最简单)