?
1.?????? Struts2的JSON插件,让Struts2也REST起来
利用Struts2的json插件――struts2-json-plugin-2.1.8.jar(在Struts-all包中有此插件包)
首先看struts.xml的配置
<!DOCTYPE struts PUBLIC ??????? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ??????? "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> ? ??? <!-- 配置Struts2应用的编码集 --> ??? <constant name="struts.objectFactory" value="spring" /> ??? <constant name="struts.i18n.encoding" value="UTF-8" /> ??? <constant name="struts.enable.DynamicMethodInvocation" value="true" /> ??? <constant name="struts.devMode" value="false" /> ? ??? <!--基类包--> ??? <package name="baseAdminJsonackage" extends="json-default" ?????? abstract="true"> ? ?????? <global-results> ? ?????????? <!--没有权限--> ?????????? <result name="hasNoToken" type="redirect"> ????????????? hasNoToken.jsp ?????????? </result> ? ?????????? <!--进入页--> ?????????? <result name="input" type="redirect">input.jsp</result> ? ?????????? <!--错误页面--> ?????????? <result name="error" type="redirect">error.jsp</result> ? ?????????? <!--ajax返回Json信息--> ?????????? <result name="successJson" type="json"></result> ?????? </global-results> ? ??? </package> ??? ??? <!--后台管理相关--> ??? <package name="administrator" extends="baseAdminJsonackage"> ?????? ?????? <!--用户管理--> ?????? <action name="userAction" ?????????? class="module.system.action.UserAction"> ?????????? <result name="success" type="redirect"> ????????????? /admin/jqgrid.jsp ?????????? </result> ?????????? <result name="loginSuccess" type="redirect"> ????????????? /admin/main.jsp ?????????? </result> ?????????? <result name="loginError" type="redirect"> ????????????? /admin/login.jsp ?????????? </result> ?????? </action> ?????? ??? </package> </struts> |
下面来看UserAction的部分代码:
public class UserAction extends BaseAction { ? public List objectList; ? ??? @JSON ??? public List getObjectList() { ?????? return objectList; ??? } ? ??? public void setObjectList(List objectList) { ?????? this.objectList = objectList; ??? } ? ??? /** ??? ?* ------------------------临时测试用 ??? ?* ??? ?* @return ??? ?*/ ??? public String listTemp() { ? ?????? // 总记录数 ?????? objectList = uxAdminDao.findAll(); ? ?????? return "successJson"; ??? } ? } |
上面是返回到”successJson”的结果中,在web前端只要通过任何的http请求到
userAction! listTemp.action,返回的结果中就可以获得objectList变量的json格式字符串。前端如何解析,就是前端的事情了。
?