拦截器的结构
一、在struts2中拦截器的结构:
(一)定义了一个类继承AbstractInterceptor类。
(二)AbstractInterceptor类实现Interceptor接口。
(三)接口里面有三个方法:init(初始化操作)、destroy(销毁操作)、interceptor(拦截器逻辑操作)。
package qwl.com.domain;
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;
}
}
package qwl.com.domain;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
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.getContext();
if("tom".equals(user.getUsername())&&"123".equals(user.getPassword())){
actionContext.getSession().put("user", user);
return SUCCESS;
}else{
actionContext.put("msg", "用户名或密码不正确");
return INPUT;
}
}
}
package qwl.com.domain;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport{
private static final long serialVersionUID = 1L;
public String add(){
System.out.println("book add");
return SUCCESS;
}
public String delete(){
System.out.println("book delete");
return SUCCESS;
}
public String update(){
System.out.println("book update");
return SUCCESS;
}
public String find(){
System.out.println("book find");
return SUCCESS;
}
}
package qwl.com.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();
Object user = actionContext.getSession().get("user");
if(user != null){
return invocation.invoke(); //继续向下执行
}else{
actionContext.put("msg", "您还未登录,请先登录");
return Action.LOGIN;//如果不存在,返回login值
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name = "struts2" namespace = "/" extends = "struts-default">
<interceptors>
<interceptor name = "privilege" class = "qwl.com.interceptor.PrivilegeInterceptor"/>
<interceptor-stack name = "myStack">
<interceptor-ref name = "defaultStack" />
<interceptor-ref name = "privilege" />
</interceptor-stack>
</interceptors>
<action name = "login" class = "qwl.com.domain.LoginAction">
<result>main.jsp</result>
<result name = "input">/login.jsp</result>
</action>
<action name = "book_*" class = "qwl.com.domain.BookAction" method = "{1}">
<result>/success.jsp</result>
<result name = "login">/login.jsp</result>
<interceptor-ref name = "myStack" />
<allowed-methods>update,delete,add,find</allowed-methods>
</action>
</package>
</struts>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>login</title>
</head>
<body>
<center>
${requestScope.msg}<br>
<form action = "login.action" method = "post">
<table>
<tr>
<td><label style = "text-align:right;">用户名:</label></td>
<td><input type = "text" name = "username"></td>
</tr>
<tr>
<td><label style = "text-align:right;">密码:</label></td>
<td><input type = "password" name = "password"></td>
</tr>
<tr>
<td align = "right" colspan = "2">
<input type = "submit" value = "登录"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@ 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>main.jsp</title>
</head>
<body>
<a href = "book_add">book add</a><br>
<a href = "book_delete">book delete</a><br>
<a href = "book_update">book update</a><br>
<a href = "book_find">book find</a><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>成功页面</title>
</head>
<body>
用户${user.username}操作成功 <br>
</body>
</html>