日期:2014-05-16 浏览次数:20354 次
?
JSON+Struts2 例子
?
在myeclipse8.6的系列中,当添加struts2框架中,自动添加的JSON的包。
?
JSON的官网: ?http://www.json.org/
?
文档的参考地址为:
?
https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin
?
?
struts.xml 配置文件
?
?
<package name="test" extends="json-default"> <action name="jsonaction" class="com.action.json.JsonAction"> <result type="json" > <!-- 排除属性 不显示userid和userage这两个字段 --> <param name="excludeProperties">user.userid,user.userage</param> </result> </action> </package>
?
action:?
?
?
@Controller public class JsonAction extends ActionSupport { @Resource private IUserDao iud; private Users user; public Users getUser() { return user; } public void setUser(Users user) { this.user = user; } public String execute() { Integer uid = user.getUserid(); // 获取JSP页面中select列表的传递过来的用户ID this.user = iud.findById(uid); // 根据用户的ID获取用户实体 return SUCCESS; // 最好直接返回SUCCESS } }
?
?
JSP页面:
?
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>xmlAction</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src="jQuery/jquery-1.5.2.js"> </script> <script type="text/javascript"> function getjsoninfo() { var val = $("#person").val(); $.ajax( { type:"post", url : "jsonaction.action", data : "user.userid=" + val, success : function(data) { alert(data.user.username); //该user必须和action中的属性同名! } }); } </script> </head> <body> <center> <s:select id="person" list="userlist" listKey="userid" listValue="username"&