Struts2与Json

首先,要添加ezmorph-1.0.3.jar和morph-1.1.1.jar这两个jar。

然后在struts.xml里面,添加json-default

<package name="default" extends="struts-default,json-default">
<action name="TypeAction_*" class="TypeAction" method="{1}">
    <result name ="VIEW_SUCCESS" >LoadPage/type.jsp</result>
    <result name ="ADD_SUCCESS" >CommonPage/ajaxDoneType.html</result>
    <result name ="ADD_FAILED" >CommonPage/ajaxDoneFailed.html</result>
    <result name ="DELETE_SUCCESS" >CommonPage/ajaxDoneType.html</result>
    <result name = "UPDATEPRO_SUCCESS" >DealPage/typeUpdate.jsp</result>
    <result name = "UPDATE_SUCCESS" >CommonPage/ajaxDoneType.html</result>
</action>
<action name="BuildingAction_*" class="BuildingAction" method="{1}">
    <result name ="VIEW_SUCCESS" >LoadPage/building.jsp</result>
    <result name ="ADD_SUCCESS" >CommonPage/ajaxDoneBuilding.html</result>
    <result name ="ADD_FAILED" >CommonPage/ajaxDoneFailed.html</result>
    <result name ="DELETE_SUCCESS" >CommonPage/ajaxDoneBuilding.html</result>
    <result name = "UPDATEPRO_SUCCESS" >DealPage/buildingUpdate.jsp</result>
    <result name = "UPDATE_SUCCESS" >CommonPage/ajaxDoneBuilding.html</result>
    <result name = "ALLSELECT_SUCCESS" >DealPage/buildingAdd.jsp</result>
    <result name = "SEARCHPRO_SUCCESS" >SearchBuildingPage/buildingSearch2.jsp</result>
    <result name = "DETAILSELECT_SUCCESS" type = "json">
      <param name="root">detailresults</param>
      <param name="contentType">application/json</param>
    </result>
    <result name = "SEARCH_SUCCESS" type = "json">
      <param name="includeProperties">buildingresults.*,pagerresult.*</param>
      <param name="contentType">application/json</param>
    </result>
    <result name = "SEARCH2_SUCCESS" >/SearchBuildingPage/building.jsp</result>
    <result name = "SEARCH3_SUCCESS" >SearchBuildingPage/buildingSearch2.jsp</result>
</action>
</package>

当然,也可以把struts-default和json-default分开写,我是因为,方便查看,所以写到一起了。

    另外,上面的action用的了匹配。如TypeAction_*:TypeAction是action类的名称,* 是此类里的方法名称,* 的数量也可以,进一步添加的,匹配的东西也就相应增多。

includeProperties与root的不同是,对于数据,root是从根节点开始。includeProperties是从根包含的元素开始,相当于去除了根。

posted @ 2013-06-13 22:45  idealing  阅读(278)  评论(0编辑  收藏  举报