struts2 默认的action访问后缀是action 和无后缀
struts的配置文件的package中的内容的配置有顺序
The content of element type "package" must match "(result- types?, interceptors?, default-interceptor-ref?, default-action- ref?, default-class-ref?, global-results?, global-exception- mappings?, action*)"。
struts2拦截器只会拦截action层,不是action请求不会拦截,拦截action会在 web.xml 中的<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> 前拦截action,当此action不存在时,并不会再次执行web.xml中的<error-page>。
当 拦截器拦截action后,可以加一个<default-action-ref name="not404"></default-action-ref>默认的跳转页面。注意,此配置的name指的是配置的action的name
,不是指的result。
不加namespace,是全局所有的action请求都可以用的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC</struts>
java**************
package com.oracle.jingdong.filter;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.oracle.jingdong.entity.Buyer;
public class UserLoginFilter extends ActionSupport implements Interceptor{
private static final long serialVersionUID = 1L
public void init() {
System.out.println("chushihua");
}
public void destory() {
System.out.println("销毁");
}
public void destroy() {
// TODO Auto-generated method stub
}
public String intercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
HttpServletRequest request=ServletActionContext.getRequest();
Buyer buyer=(Buyer)request.getSession().getAttribute("buyerName");
String requestName=ai.getInvocationContext().getName();
System.out.println(requestName);
if(buyer!=null)
return ai.invoke();
else if(requestName.equals("JdManAct_login"))
return ai.invoke();//ai.invoke()是String类型,值为sucess
else
return "loginError";
}
}