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> <interceptors> <interceptor name="loginInterceptor" class="com.kaishengit.web.interceptor.LoginInterceptor"/> <!-- json --> <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/> <interceptor name="jsonValidation" class="org.apache.struts2.json.JSONValidationInterceptor"/> <!-- 拦截器栈名采用Struts-json插件的栈名 --> <interceptor-stack name="jsonValidationWorkflowStack"> <interceptor-ref name="loginInterceptor"> <param name="sessionName">employee</param> <param name="excludeActionName">index,login</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <!-- json --> <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> <default-interceptor-ref name="jsonValidationWorkflowStack"/> <global-results> <result name="login" type="redirectAction">index.action</result> </global-results> </package> </struts>
三、注解使用Json,一个例子
package com.kaishengit.action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import com.kaishengit.pojo.Employee; import com.kaishengit.pojo.FileType; import com.kaishengit.pojo.Message; import com.kaishengit.pojo.Project; import com.kaishengit.service.EmployeeService; public class CopyOfAjaxAction extends BaseAction{ private static final long serialVersionUID = 1L; private EmployeeService employeeService; private List<Employee> employeeList; private Employee employee; private String id; private Message message; private Project project; private FileType fileType; /** * 显示employee */ //注解使用方式↓ @Action(value="employeeJson",results={@Result(name="success",params={"root","employeeList","noCache","true","enableGZIP","true","excludeNullProperties","true"},type="json")}) public String execute(){ employeeList = employeeService.findAll(); //定制json List<Employee> jsonList = new ArrayList<Employee>(); for (Employee employee : employeeList) { Employee jsonEmployee = new Employee(); jsonEmployee.setId(employee.getId()); jsonEmployee.setUsername(employee.getUsername()); jsonList.add(jsonEmployee); } employeeList = jsonList; return "success"; } //get set public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public Message getMessage() { return message; } public void setMessage(Message message) { this.message = message; } public Project getProject() { return project; } public void setProject(Project project) { this.project = project; } public FileType getFileType() { return fileType; } public void setFileType(FileType fileType) { this.fileType = fileType; } @Autowired public void setEmployeeService(EmployeeService employeeService) { this.employeeService = employeeService; } }
end...