读书人

Struts应用全注解配置拦截器(Interc

发布时间: 2012-07-05 07:59:18 作者: rapoo

Struts使用全注解,配置拦截器(Interceptor)和注解配置Json(struts-json插件)

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" name="code">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值@Overridepublic 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 setpublic 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" 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();//定制jsonList<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 setpublic 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;}@Autowiredpublic void setEmployeeService(EmployeeService employeeService) {this.employeeService = employeeService;}}
??

ok,很easy吧...

读书人网 >JavaScript

热点推荐