关于Struts-JSON的提高开发效率
一、JSON是什么?
:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解
析和生成。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。
二 、struts2对于JSON有什么支持?
:struts2提供了一个struts2-json-plugin.jar 用于支持JSON的使用。当我们在struts2中使用JSON的时候
必须导入struts2-json-plugin.jar
并且需要一下几点:
1.<package name="struts2" extends="json-default">
//必须是继承struts2-json-plugin.jar中struts-plugin.xml文件中的定义的json-default
/* struts2-json-plugin.jar中struts-plugin.xml配置信息:
<struts>
<package name="json-default" extends="struts-default">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
<interceptors>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/> </interceptors>
</package>
</struts>
通过配置信息我们可以知道:
1.json-default其实是继承了struts-default的
2.定义了一个name为json的返回类型和一个name为json的拦截器 */
2.<action name="*" class="*"><result name="success" type="json"/></action>//我们需要将result
的 返回类型定义为json
三、关于JSON在struts.xml中的一些属性的功能。
<action name="*" class="*">
<result name="success" type="json">
<param name=""></param>//关于对JSON的一些定义
/*excludeProperties //代表排除Action中的哪些属性,排除多个属性时,使用逗号进行分隔(即不需要序列化的属性)
例:<param name="excludeProperties">age</param> 排除getAge()这个方法 为什么是排除这个方法下面详解
includeProperties //代表包含Action中的哪些属性,包含多个属性是,使用逗号进行分隔(即需要序列化的属性)
例:<param name="includeProperties">address</param>包含getAddress()这个方法 为什么是包含这个方法下面详解
excludeNullProperties //代表排除Action中属性值为空的字段
例:<param name="excludeNullProperties">true</param> 使用true/false 默认为false(即包含属性值为null的字段)
root //代表从哪里开始序列化
例:<param name="root">person</param> person对应的可以是一个属性,也可以是一个对象,也可以是一个集合
//当在这里定义后,只会将person进行序列化,如果person是一个对象,则会将person对象中所有的getter方法都进行序列化
注意:如果相同属性同时定义了excludeProperties和includeProperties
那么excludeProperties的优先级要高,因此不对
该属性进行序列化
*/
</result>
</action>
四、关于JSON在Action对象中注解的使用。
五、具体的实例:
Action:
package org.viancent.action;
import java.util.Date;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class GetJsonAction extends ActionSupport {
private String username;
private String password;
private int age;
private Date date;
@JSON(serialize = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@JSON(name="mypassword")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@JSON(format="yyyy-MM-dd")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String execute() throws Exception {
this.setDate(new Date());
this.setAge(10);
this.setPassword("world");
this.setUsername("hello");
return SUCCESS;
}
}
struts.xml
<struts>
<package name="struts2" extends="json-default">
//这里一定要继承json-default
<action name="getjsons" class="org.viancent.action.GetJsonAction">
<result name="success" type="json">
//这里一定要定义type属性为json
<param name="excludeProperties">age</param>
//排除getAge()方法
</result>
</action>
</package>
</struts>
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="WEB-JS/jquery-1.5.1.js"></script> //使用jquery一定要导入js
<script type="text/javascript"> //使用jquery实现异步提交
function getJson() {
$.post(
"/struts2/getjsons.action",{},function(returnedData,status)
{
if("success"==status)
{
alert(returnedData);
}
}
)
}
</script>
</head>
<body id="theBody">
<input type="button" value="GetJson" onclick="getJson();">
</body>
</html>
得到的结果为:{"date":"2011-03-21","mypassword":"world"}
本文介绍了Struts2框架如何集成JSON插件以提高开发效率。涵盖了struts2-json-plugin的配置方法,如何在struts.xml中配置JSON相关属性及在Action类中使用JSON注解。并提供了一个具体示例来展示如何排除、包含特定属性以及如何改变日期格式。
780

被折叠的 条评论
为什么被折叠?



