1、下载struts2相应jar包
http://struts.apache.org/download.cgi#struts-extras
http://archive.apache.org/dist/struts/binaries/
2、新建 JavaWeb工程
3、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts2</display-name> <welcome-file-list> <welcome-file>/WEB-INF/pages/organization/employee.jsp</welcome-file> </welcome-file-list> <!-- struts2启动器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
4、在src 下新建struts.xml文件,(本来想放在其他文件下通过web.xml文件<init-param>配置访问路径的,但是一直启动报错,只好放在src下了,查资料说好像和struts的版本有关系,反正不是什么重要的问题,就放过了)。
可以通过引用其他struts.xml文件,更加方便条理的管理struts配置文件(即,每个不同的模块包创建一个struts配置文件,例如:employee_struts.xml,之后通过<include>添加到struts.xml文件中)
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <include file="conf/employee/employee_struts.xml"></include> </struts>
5、编写访问action。此action返回json数据,需要创建一个private Map<String,Object> resultMap; 并且创建它的get、set方法。
在action中为resultMap赋值,再将resultMap放在上下文中: ActionContext.getContext().put("pageView", resultMap);
(pageView可以自己定义但必须和struts配置文件中 <param name="root">pageView</param>一致,root是ognl表达式定义的,不要更改)
package com.sxdx.employee.action; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import com.opensymphony.xwork2.ActionContext; public class employeeAction { private static final Logger log = Logger.getLogger(employeeAction.class); private Map<String,Object> resultMap; public Map<String, Object> getResultMap() { return resultMap; } public void setResultMap(Map<String, Object> resultMap) { this.resultMap = resultMap; } public String showEmployeeList(){ /*JSONObject cell = new JSONObject(); result = cell.toString();*/ resultMap = new HashMap<String, Object>(); resultMap.put("eid", "002"); resultMap.put("ename", "张三"); resultMap.put("esex","男" ); resultMap.put("eage","22" ); ActionContext.getContext().put("pageView", resultMap); return "success"; } }
6、创建模块包级别的struts配置文件(此文件需要在第4部创建的文件中被引用才可生效)
employee_struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="employee" namespace="/employee" extends="json-default"> <action name="employeeAction_*" class="com.sxdx.employee.action.employeeAction" method="{1}"> <result name="success" type="json"> <param name="root">pageView</param> </result> </action> </package> </struts>
7、访问路径
http://localhost:8080/struts2/employee/employeeAction_showEmployeeList.action
8、结果
{"esex":"男","eage":"22","ename":"张三","eid":"002"}
注意:在返回json数据时需要struts2-json-plugin-2.3.4.jar 包的支持,但是一定要注意版本问题。