利用注解配置拦截器:
在xwork包下有:
com.opensymphony.xwork2.interceptor.annotations.After.class
com.opensymphony.xwork2.interceptor.annotations.Before.class
com.opensymphony.xwork2.interceptor.annotations.BeforeResult.class
com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor.class
它们是用来配置annotation的。其中前三个依赖于AnnotationWorkflowInterceptor。下面用例子介绍一下:
<%@ taglib prefix="s" uri="/struts-tags" %> <body> <s:form method="POST" action="login.action"> <s:textfield name="user.username" label="Username:"></s:textfield> <s:password name="user.password" label="Password:"></s:password> <s:submit value="提交"></s:submit> </s:form> </body>
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<?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="annotation" extends="struts-default"> <interceptors> <interceptor name="annotationInterceptor" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"></interceptor> </interceptors> <action name="login" class="com.zchen.action.LoginAction" > <result name="success">index.jsp</result> <interceptor-ref name="annotationInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
package com.zchen.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
import com.opensymphony.xwork2.interceptor.annotations.BeforeResult;
import com.zchen.model.User;
public class LoginAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 4387832093273420762L;
private User user = null;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("action方法");
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return super.execute();
}
@Before
public void doBefore() {
System.out.println("action执行之前方法被调用");
}
@After
public void doAfter() {
System.out.println("action执行之后方法被调用");
}
@BeforeResult
public void doBeforeResult() {
System.out.println("result执行之前方法被调用");
}
public void setSession(Map<String, Object> arg0) {
}
}
package com.zchen.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;
}
}
此时输出顺序是:
action执行之前方法被调用
action方法
admin
1234
result执行之前方法被调用
action执行之后方法被调用
所以我们可以用这种配置判断用户是否登录,只有登录了才可以访问页面:
(我们单写一个Action类作为权限管理简要代码如下)
package com.zchen.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class AnthInterceptor extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 4387832093273420762L;
Map session;
public String doBefore(){
if(session.get("login")==null){
//如果没有session表示用户还没有登录
return Action.LOGIN;
}else{
//返回null表示程序继续执行execute方法而此方法默认返回success
return null;
}
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
在struts.xml中配置即可。