日期:2014-05-16 浏览次数:20559 次
Struts注解插件和struts-Json插件都默认继承struts-default包,导致无法同时使用注解,因为Action类只能有一个父包,配置其中一个都会把另一个给覆盖掉,嘿嘿,不用担心,只需要把struts-json插件中的默认配置都拷到struts.xml配置文件即可
?
配置步骤:
一、查看struts-json插件中的默认配置文件:struts-plugin.xml代码:
?
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="json-default" extends="struts-default"> <result-types> <result-type name="json" class="org.apache.struts2.json.JSONResult"/> </result-types> <interceptors> <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/> <interceptor name="jsonValidation" class="org.apache.struts2.json.JSONValidationInterceptor"/> <!-- Sample JSON validation stack --> <interceptor-stack name="jsonValidationWorkflowStack"> <interceptor-ref name="basicStack"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel</param> </interceptor-ref> <interceptor-ref name="jsonValidation"/> <interceptor-ref name="workflow"/> </interceptor-stack> </interceptors> </package> </struts>
?二、把上面配置信息全部都拷到struts.xml中
1>我的拦截器代码,一个简单的登录拦截器:LoginInterceptor.java
?
package com.kaishengit.web.interceptor; import java.util.Map; import java.util.Set; import com.kaishengit.pojo.Employee; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.opensymphony.xwork2.util.TextParseUtil; public class LoginInterceptor extends AbstractInterceptor{ private static final long serialVersionUID = 1L; private String excludeActionName;//剔除的拦截方法 private String sessionName;//用户名在session中存放的key值 @Override public String intercept(ActionInvocation invocation) throws Exception { String actionName = invocation.getProxy().getActionName();//获取当前访问的action名字 Set<String> set = TextParseUtil.commaDelimitedStringToSet(excludeActionName); if(set.contains(actionName)){ return invocation.invoke(); }else{ Map<String, Object> session = invocation.getInvocationContext().getSession(); Employee employee = (Employee) session.get(sessionName); if(employee == null){ return "login";//没有登录,跳转到登录页 }else{ return invocation.invoke(); } } } //get set public String getExcludeActionName() { return excludeActionName; } public void setExcludeActionName(String excludeActionName) { this.excludeActionName = excludeActionName; } public String getSessionName() { return sessionName; } public void setSessionName(String sessionName) { this.sessionName = sessionName; } }
?2>配置struts.xml配置文件
?
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.convention.result.path" value="/WEB-INF/views"/> <!--配置Struts-convention注解的默认父包 --> <constant name="struts.convention.default.parent.package" value="myPackage"/> <!--继承Struts-convention注解插件的xml --> <package name="myPackage" extends="convention-default"> <!-- 把Struts-json插件默认配置文件代码全部有序copy过来 --> <!-- json --> <result-types> <result-type name="json" class="org.apache.struts2.json.JSONResult"/> </result-types> <interc