1在HeadFirstStruts2chapter03中,新建com.cruise.model包,新建User类,属性userName,password,
package com.cruise.model;
public class User {
private String userName;
private String password;
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;
}
}
2新建com.cruise.service包,新建类:UserService类,模拟业务层,写登陆验证逻辑
package com.cruise.service;
import com.cruise.model.User;
public class UserService {
public boolean login(User user){
if ("cruise".equals(user.getUserName())&& "123456".equals(user.getPassword())) {
return true;
}else{
return false;
}
}
}
3新建UserAction,User属性(通过JavaBean的属性注入),UserService属性,error属性,在execute方法中逻辑判断,放在session中
package com.cruise.action;
import java.util.Map;
import com.cruise.model.User;
import com.cruise.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private User user;
private UserService userService =new UserService();
private String error;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
@Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
boolean login2 = userService.login(user);
if(login2){
session.put("currentUser", user);
return SUCCESS;
}else{
this.error="用户名和密码错误";
return "error";
}
}
}
5新建ZiYuanAction,只有登陆的时候才能访问
package com.cruise.action;
import com.opensymphony.xwork2.ActionSupport;
public class ZiYuanAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("当您登陆成功的时候才可以查看本资源。。。");
return "ziyuan";
6struts.xml中继续定义ZiYuanAction,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="manage" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor"class="com.cruise.interceptor.LoginInterceptor"></interceptor>
</interceptors>
<action name="user" class="com.cruise.action.UserAction" >
<result name="success" >success.jsp</result>
<result name="error" >error.jsp</result>
</action>
<action name="ziyuan" class="com.cruise.action.ZiYuanAction" >
<result name="error" >error.jsp</result>
<result name="ziyuan" >ziyuan.jsp</result>
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
7定义LoginInterceptor,获取session,判断session是否有值,如果有值,继续调用;如果没有值,创建request。将错误信息放在request域中。同时返回字符串"error",拦截器中的"error"也能映射到struts.xml文件中的ZiYuanAction中的<result>标签。
package com.cruise.interceptor;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.cruise.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor{
@Override
public void destroy() {
System.out.println("LoginInterceptor销毁");
}
@Override
public void init() {
System.out.println("LoginInterceptor初始化");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("在Action执行之前。。。");
ActionContext actionContext = invocation.getInvocationContext();
Map<String, Object> session = actionContext.getSession();
Object object = session.get("currentUser");
String result=null;
if(object!=null){
invocation.invoke();
}else{
ActionContext actionContext2 = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest)actionContext2.get(ServletActionContext.HTTP_REQUEST);
request.setAttribute("error", "未登陆,不能查看资源");
result= "error";
}
System.out.println("在Action执行之后。。。");
return result;
}
}
8login.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="user" method="post">
登陆<br>
用户名:<input type="text" name="user.userName"/><br>
密码:<input type="password" name="user.password" /><br>
<input type="submit" value="登陆" />
</form>
如果您已经登陆,请点击查看资源:<a href="ziyuan">资源</a>
</body>
</html>
9error.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>
错误消息提示:${error}
<a href="login.jsp">登录</a>
</body>
</html>
success.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>
用户:${user.userName},登录成功!!!
现在可以查看资源了<a href="ziyuan">资源</a>
</body>
</html>
ziyuan.jsp文件,这里的用户信息是从session获取
<%@ 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>
您好:${currentUser.userName }<br>
这里是您要查看的资源
</body>
</html>
10struts.xml优化:修改默认的拦截器栈的方式,把自己的拦截器放到默认的拦截器中,因为自己的拦截器有验证功能,但是当我们发送登陆请求的时候就不能有验证,否则永远登陆不了。
ps:本例中,error的内容有两个,一个是在登陆失败的时候在UserAction中设置的“用户名或密码错误”,这个信息是通过session设置的;还有一个是被拦截的时候的错误信息,提示“请先登录”,这个信息是通过request方式设置的。说明request和session在本例总作用是相同的。
登陆成功后,回显的用户名也是从session中获取的。
struts.xml优化:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="manage" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor"class="com.cruise.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-results>
<result name="error" >error.jsp</result>
</global-results>
<action name="user" class="com.cruise.action.UserAction" >
<result name="success" >success.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="ziyuan" class="com.cruise.action.ZiYuanAction" >
<result name="ziyuan" >ziyuan.jsp</result>
</action>
</package>
</struts>
第19讲 struts2拦截器-登陆验证
最新推荐文章于 2022-08-24 19:30:00 发布