现在的位置: 首页 > 综合 > 正文

Structs2+Spring或SpringMVC+JQuery+JSon 实现Json数据返回的若干研究结论

2017年11月30日 ⁄ 综合 ⁄ 共 2639字 ⁄ 字号 评论关闭

 

结论一:

<action name="" class="">
	<result name="" type="json">
	</result>
</action>

如果在struts.xml中有以上代码,那么Action中的东西将无法再用Struts2的标签取到,利用<s:debug></s:debug>你可以观察到此时Value Stack Contents和Stack Context根本不存在Action中的属性、方法等东西。

 

结论二:

关于<result type="json">下<paramt></param>标签的参数配置问题。

<result type="json">
    <!-- root参数只会返回根层对象,以前我总是自己将Action中要返回前台的数据(如List)封装在jsonResult,然后配置root,可这样防线无法取到Acion中的其它东西,所以现在用结论三和四里的方法了-->
    <param name="root">jsonResult</param>
    <!-- 指定是否序列化空的属性 -->
    <param name="excludeNullProperties">true</param>
    <!-- 这里指定将序列化dataMap中的那些属性 -->
    <param name="includeProperties">
         userList.*
    </param>
    <!-- 这里指定将要从dataMap中排除那些属性,这些排除的属性将不被序列化 -->
    <param name="excludeProperties">
         SUCCESS
    </param>
</result>

 

 

结论三-配置实例:

Action部分代码

	private String name;
	private List<Dept> deptList;  
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Dept> getDeptList() {
		return deptList;
	}
	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}
	    

 

struts.xml部分代码

<package name="s2sh_json" extends="json-default" namespace="/departmentJSon">
	<action name="deptJSon" class="deptJSon">
		<result name="deptListWithJSon" type="json">
			<param name="includeProperties">name,deptList.*</param>
		</result>
	</action>
</package>

注意:extends必须是json-default;

includeProperties中Action想要以json数据格式返回的属性除了String等基本类型外,其它都必须加.*(如此处的deptList.*,当然如果有valueBean也要这样);

项目中必须加入struts2-json-plugin.jar和commons-lang.jar这两个jar包。

 

Jsp中JQery部分代码

$.ajax({cache:false,
	type:"post",
	dataTyp:"json",
	url:"<%=basePath%>departmentJSon/deptJSon!getAllDeptByAjax.action",
	success:function(data) {
		alert(data.name);				
		$.each(data.deptList, function(i, one) {
			result = "<tr align='center'><td>"+one.dno+"</td><td>"+one.dname+"</td><td>"+one.dloc+"</td></tr>";
			$(result).appendTo("#deptListDiv");
		});
	}	
});

 

结论四-另一个配置实例:

如果不喜欢在struts.xml中配置<param name="includeProperties">name,deptList.*</param>,那么可以采用在Action写annotation,可以达到相同的效果。

struts.xml的配置如结论三中的,但是去掉<param name="includeProperties">name,deptList.*</param>。

而Action部分代码如下,关于@JSON中参数设置可以自己百度,常用的也就serialize。

          private String name;
	private List<Dept> deptList;  
	@JSON(serialize=true)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
        @JSON(serialize=true)
	public List<Dept> getDeptList() {
		return deptList;
	}
	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

 

结论五-SpringMVC中的配置实例:
 在springmvc中由于@ResponseBody返回的是可序列化的对象,因此只要将结果利用一些工具类事先转换为json对象,那么就能够返回给前端。

 

@RequestMapping(value = "/closingDetailView")
	@ResponseBody
	public JSONArray closingDetailView(@RequestParam(value = "closingId", required = true) String closingId)
	{
		List<ClosingDetail> detailList = closingDetailService.queryClosingDetailForListByClosingId(closingId);
		JSONArray jsonArray = JSONArray.fromObject(detailList);
		return jsonArray;
	}

抱歉!评论已关闭.