配置和使用拦截器
在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include file="struts-default.xml" />”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。
例如:UserAction.java
package com.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
private String pass;
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
if (getName().equals("admin") && getPass().equals("123")) {
Map sessionMap = actionContext.getSession();
sessionMap.put("name", getName());
return SUCCESS;
} else {
actionContext.put("tip", "你的账号和密码不对,已拦截!");
return ERROR;
}
}
}
















