struts2验证用户是否登陆
2017-02-16 00:00
375 查看
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginaction" method="post">
<center>
<h2>用户登录</h2>
${loginerror}
用户:<input type="text" name="username"><br><br>
</form>
</body>
</html>
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" namespace="/" extends="struts-default">
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="validate" class="com.validate.lan.AuthorIntercept"></interceptor>
</interceptors>
<action name="author">
<result>/login.jsp</result>
<result name="login">/login.jsp</result>
</struts>
3LoginAction.java
package com.validate.action;
import java.util.Map;
import javax.jms.Session;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware{
private String username;
public String getUsername() {
return username;
}
}
4 AuthorIntercept.java
package com.validate.lan;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorIntercept extends AbstractInterceptor {
}
5 小知识 自定义拦截器栈
<interceptors>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginaction" method="post">
<center>
<h2>用户登录</h2>
${loginerror}
用户:<input type="text" name="username"><br><br>
密码:<input type="password" name="password"><br><br> <input type="submit" value="提交"> </center>
</form>
</body>
</html>
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" namespace="/" extends="struts-default">
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="validate" class="com.validate.lan.AuthorIntercept"></interceptor>
</interceptors>
<action name="author">
<result>/login.jsp</result>
<result name="login">/login.jsp</result>
<!-- 引用拦截器 --> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="validate"></interceptor-ref> </action> <action name="loginaction" class="com.validate.action.LoginAction" method="login"> <result name="success">/manager.jsp</result> <result name="error">/login.jsp</result> </action> </package>
</struts>
3LoginAction.java
package com.validate.action;
import java.util.Map;
import javax.jms.Session;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware{
private String username;
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; } private String password; private Map<String,Object> session; public void setSession(Map<String, Object> session) { this.session=session; } public String login(){ if("123".equals(username)&&"123".equals(password)){ session.put("logininfo", username); return SUCCESS; }else{ session.put("loginerror", "用户名或密码错误"); return ERROR; } }
}
4 AuthorIntercept.java
package com.validate.lan;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorIntercept extends AbstractInterceptor {
@Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext context=ActionContext.getContext(); Map<String,Object> session=context.getSession(); if(session.get("logininfo")!=null){ String result=invocation.invoke(); return result; }else{ return "login"; } }
}
5 小知识 自定义拦截器栈
<interceptors>
<interceptor name="validate" class="com.validate.lan.AuthorIntercept"></interceptor> <interceptor-stack name="mystack"> <interceptor-ref name="validate"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors>
相关文章推荐
- spring mvc 异常处理手动回滚 SQL log不回滚
- 类的注意点
- HashMap工作原理
- 统计用户在线登录人数
- java 关于查询列表中含有子集的处理;
- java 异常 你又了解几分
- 分析下为什么spring 整合mybatis后为啥用不上session缓存
- 121 项目 014 笔记向 Java中的escape,unescape方法
- JavaSE 学习参考:数组遍历
- 博为峰JavaEE技术文章 ——MyBatis 简单应用
- 博为峰Java技术文章 ——JavaSE Swing 适配器类
- 使用Spring4.3解决缓存过期后多线程并发访问数据库的问题
- Spring Cache抽象详解
- Spring3 Cache相关注解说明及实例
- java线程的实现方式
- eclipse 签名打包时 在手机上闪退
- myeclipse10.5下载以及破解
- Java代码执行顺序
- [转]Java 的强引用、弱引用、软引用、虚引用
- spring整合activeMq