Java EE SSH框架之Struts2 (6)—— Struts2中自定义拦截器
程序员文章站
2022-07-09 17:15:37
...
当用户登陆后才能对“book”进行”增删改查“的小例子(此例中没有涉及连接数据库)
下面是项目的目录结构:
首先,在web.xml里注册struts2
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2_StudyDemo</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
</web-app>
写一个简单的User类:
package com.zl.domin;
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;
}
}
struts.xml
自定义拦截器需要声明,并且在Action里面注册才有效!!!
<?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>
<constant name="struts.devMode" value="true" />
<package name="IntercepterStruts2Demo" namespace="/" extends="struts-default">
<!-- 声明拦截器 -->
<interceptors>
<interceptor name="privilege" class="com.zl.interceptor.PrivilegeInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="privilege"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 用户登录操作 -->
<action name="login" class="com.zl.action.LoginAction">
<result>/main.jsp</result>
<result name="input">/login.jsp</result>
</action>
<!-- 关于book操作 -->
<action name="book_*" class="com.zl.action.BookAction" method="{1}">
<result>/success.jsp</result>
<result name="login">/login.jsp</result>
<!-- 在Action中使用自定义拦截器 -->
<interceptor-ref name="myStack"/>
</action>
</package>
</struts>
控制用户登录的LoginAction.java
package com.zl.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zl.domin.User;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 1L;
private User user = new User();
public User getModel() {//模型驱动
return user;
}
@Override
public String execute() throws Exception {
//获取ActionContext
ActionContext actionContext = ActionContext.getContext();
if("Cookie".equals(user.getUsername()) && "12345678".equals(user.getPassword())) {
//将用户存储在session中
actionContext.getSession().put("user", user);
return SUCCESS;
} else {
actionContext.put("msg", "用户或密码不正确!!!");
return INPUT;
}
}
}
main.jsp
<%@ page language="java" import="java.util.*" 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>main.jsp</title>
</head>
<body>
<a href="<%=request.getContextPath()+"/book_del" %>">book del</a><br>
<a href="<%=request.getContextPath()+"/book_add" %>">book add</a><br>
<a href="<%=request.getContextPath()+"/book_update" %>">book update</a><br>
<a href="<%=request.getContextPath()+"/book_find" %>">book find</a><br>
</body>
</html>
用户登录界面login.jsp
<%@ page language="java" import="java.util.*" 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>1550350070周丽_login.jsp</title>
</head>
<body>
<center>
${requestScope.msg}<br>
<form action="<%=request.getContextPath()+"/login.action" %>" method="post">
<table>
<tr>
<td><label style="text-align:right;">用户名:</label></td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td><label style="text-align:right;">密码:</label></td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td><input value="登录" type="submit"></td>
<td><input value="重置" type="reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
对book增删改查选择的BookAction.java
package com.zl.action;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport{
public String add() {
System.out.println("book add");
return SUCCESS;
}
public String del() {
System.out.println("book del");
return SUCCESS;
}
public String update() {
System.out.println("book update");
return SUCCESS;
}
public String find() {
System.out.println("book find");
return SUCCESS;
}
}
最关键的:控制用户必须要登陆才能对book进行增删改查PrivilegeInterceptor.java
package com.zl.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class PrivilegeInterceptor extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//得到ActionContext
ActionContext actionContext = invocation.getInvocationContext();
//获取user对象
Object user = actionContext.getSession().get("user");
if(user != null) {
return invocation.invoke(); // 继续向下执行
}else {
actionContext.put("msg", "您还未登录,请先登录!");
return Action.LOGIN; //如果用户不存在,返回login值
}
}
}
(success.jsp就是一个登录成功的界面,就不占用篇幅了)上一篇: struts2中自定义拦截器
下一篇: Struts2--拦截器