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"-//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" />
<!-- Action访问方式 -->
<!-- <constant name="struts.action.extension" value="do"></constant> -->
<package name="default" extends="struts-default">
<interceptors><!-- 拦截器 -->
<interceptor name="loginInterceptor" class="com.oracle.jingdong.filter.UserLoginFilter" />
<interceptor-stack name="loginStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack" /><!--设置成 默认拦截器,注意要拦截器栈要包括 <interceptor-ref name="defaultStack"/> -->
<default-action-ref name="not404"></default-action-ref>
<global-results><!-- 错误页面 -->
<result name="error">/error.jsp</result>
<result name="loginError">/loginError.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.NullPointerException" result="error" />
<exception-mapping exception="java.sql.SQLException" result="error" />
</global-exception-mappings>
<action name="not404">
<result>/404.jsp</result>
</action>
</package>
<package name="aaa" namespace="/" extends="default">
</package>
<include file="com/oracle/jingdong/action/jd/struts-jd.xml"></include>
<!-- <include file="com/oracle/jingdong/action/buyer/struts-buyer.xml" /> -->
<!-- <include file="com/oracle/jingdong/action/jd/struts-jd.xml" />
<include file="com/oracle/jingdong/action/seller/struts-seller.xml" />
<include file="com/oracle/jingdong/action/front/struts-front.xml" /> -->
</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";
}
}