配置struts.xml
class="xml" name="code"><!-- 定义全局package --> <package name="defaultpackage" extends="struts-default"> <interceptors> <interceptor-stack name="defStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <!--把自己的拦截器添加到定义的拦截器栈里面--> <interceptor-ref name="sessionNull"></interceptor-ref> </interceptor-stack> <!--定义自己的拦截器--> <interceptor name="sessionNull" class="com.jyjsf.interceptor.SessionNullInterceptor" /> </interceptors> <!--调用拦截器--> <default-interceptor-ref name="defStack" /> <!--定义全局结果集--> <global-results> <result name="sessionnull"> /login.jsp </result> </global-results> </package>
? 添加拦截器类
public class SessionNullInterceptor extends AbstractInterceptor { /** * */ private static final long serialVersionUID = 1L; @Override public String intercept(ActionInvocation invocation) throws Exception { User u=(User) ActionContext.getContext().getSession().get("user"); if(u!=null){ return invocation.invoke(); } return "sessionnull"; } }
?所有Action类继承这个全局package
//@Component("userAciton") /* * 用户的Action类 * @author DZWei */ @ParentPackage(value = "defaultpackage")//应用全局包 @Scope("prototype") @Namespace("/") public class UserAction extends BaseAction { @Action(value = "initializationAction", results = { @Result(name = "success", location = "XXXXXXXXX"), @Result(name = "fail", location = "XXXXXXX") }) public String initialization() throws Exception { ................................................ } }
?
?