1.导包struts2-json-plugin-2.1.8.1.jar
2.(1)struts.xml
<package name="dang-default" extends="json-default" namespace="/"> <include file="config/struts/struts-user.xml" /> </package>(2)struts-user.xml
<package name="user" extends="dang-default" namespace="/user"> <action name="userAction_*" class="com.tarena.dang.web.action.UserAction" method="{1}"> ............. </action> </package>3.具体的语法在代码中体会,说再多也不如实践
<result name="displayVerifyCode" type="json"> <param name="includeProperties">user\.verifyCode</param> </result>
结果
{"user":{"verifyCode":"6de5fe9c67c140469310ee1f54e56a14-79"}}
在js中用jquery ajax处理返回来的json字符串 data.user.verifyCode (前提是dataType是json,data为success()函数的参数)
如果要访问user的全部属性,在struts.xml中
<result name="displayVerifyCode" type="json"> <param name="includeProperties">user.*</param> </result>
如果访问的的是一个List类型的.List<User> userList = new ArrayList<User>();
要返回userList中所有信息
<result name="displayVerifyCode" type="json"> <param name="includeProperties">userList.*</param> </result>
<result name="displayVerifyCode" type="json"> <param name="includeProperties">userList\[\d+\]\.email</param> </result>
结果
{"userList":[{"email":"[email protected]"},{"email":"[email protected]"}]}
(2)根对象只包含user
<result name="displayVerifyCode" type="json"> <param name="root">user</param> </result>