struts2.2.1 怎么把pojo类封装成json对象?

xq30397022 2012-04-15 06:12:37
我写了个struts2+spring+jquery+json的ajax案例,但是发现action的方法无法返回我要的pojo类
pojo代码:
package com.oa.pojo;

import java.sql.Timestamp;

/**
* Attendance entity. @author MyEclipse Persistence Tools
*/

public class Attendance implements java.io.Serializable {

// Fields
private static final long serialVersionUID = 1L;

private Integer attendanceid;
private User user;
private Timestamp intime;
private Timestamp outtime;
private String content;

// Constructors

/** default constructor */
public Attendance() {
}

/** full constructor */
public Attendance(User user, Timestamp intime, Timestamp outtime,
String content) {
this.user = user;
this.intime = intime;
this.outtime = outtime;
this.content = content;
}

// Property accessors

public Integer getAttendanceid() {
return this.attendanceid;
}

public void setAttendanceid(Integer attendanceid) {
this.attendanceid = attendanceid;
}

public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}

public Timestamp getIntime() {
return this.intime;
}

public void setIntime(Timestamp intime) {
this.intime = intime;
}

public Timestamp getOuttime() {
return this.outtime;
}

public void setOuttime(Timestamp outtime) {
this.outtime = outtime;
}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

}
action代码
package com.oa.action;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.json.annotations.JSON;

import com.oa.biz.AttendanceBiz;
import com.oa.pojo.Attendance;
import com.oa.pojo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AttendanceAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private Attendance att;
public Attendance getAtt() {
return att;
}
public void setAtt(Attendance att) {
this.att = att;
}

private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}


//获取考情业务层
private AttendanceBiz attbiz;
public AttendanceBiz getAttbiz() {
return attbiz;
}
public void setAttbiz(AttendanceBiz attbiz) {
this.attbiz = attbiz;
}

//添加签到
public String addin() throws Exception{
//msg = "============";
att = new Attendance();
Map<String, Object> session = ActionContext.getContext().getSession();
User user = (User) session.get("user");
att.setUser(user);
System.out.println("att的内容是:"+att.getContent());
System.out.println("att的用户是:"+att.getUser().getUname());
//用于插入新记录的
attbiz.addAtt(att);
return "att";
}
}
struts.xml
<!-- json package-->
<package name="struts-json" extends="json-default" namespace="/">
<action name="attjson" class="com.oa.action.AttendanceAction">
<result name="att" type="json"></result>
</action>
</package>
jsp页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
<base href="<%=basePath%>">
<sx:head />
<title>My JSP 'singe.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="styles/singe.css" type="text/css" />
<script src="scripts/singe.js" type="text/javascript"></script>
<script src="scripts/json.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-1.3.1.js"></script>
<script type="text/javascript">
// JavaScript Document
$(document).ready(function(){
$("#singein").click(function(){
alert("ssssssss");
var params = $("#form1").serialize();
$.ajax({
url:"attjson!addin.action",
type:"post",
data:params,
datatype:"json",
success:function(data){
var obj = eval( "(" + data + ")" );//转换后的JSON对象
$("#singe_in").html("");
$("#singe_in").append("<div>---更新的页面---</div>").append("<div>"+obj.msg+"</div>");
},
error:function(){
alert("error");
}
});
});
});
</script>
</head>

<body>

<div id="oprate_top">
<h4 align="center">员工签到、签退</h4>
<hr color="#666666"/>
</div>

<fieldset>
<legend>员工填写区</legend>
<form name="form1" id="form1" >
<table width="500" border="0" cellpadding="1" >
<tr>
<td width="94" align="center">签卡日期:</td>
<td width="222"><sx:datetimepicker name="att.intime" displayFormat="yyyy-MM-dd"></sx:datetimepicker></td>
<td width="96" align="center"><a id="" ><img src="images/singe_in..gif" /></a><input id="singein" type="button" value="qiandao"/> </td>
<td width="70" align="center"><a id="singeout" ><img src="images/singe_out..gif" /></a></td>
</tr>
<tr>
<td align="center">签卡备注:</td>
<td colspan="3"><textarea name="att.content" id="content" cols="45" rows="5"></textarea></td>
</tr>
</table>
</form>
</fieldset>

<hr color="#66FFCC" width="100%"/>

<div id="singe_in"></div>



</body>
</html>
...全文
327 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjoy4856 2012-04-23
  • 打赏
  • 举报
回复
实现Json的两种方式:
1.自己处理特殊的(自定义的Json)
2.使用插件

1.自定义:
要知道Json是什么样的格式,每个对象在Json中是怎么样的形式
2.使用插件
json-lib-2.3-jdk15.jar 还有其他的版本,
使用方法JsonArray.formate(Object).toString()
这里有一个问题就是能不能处理循环嵌套的对象(有时候行)有时候不清楚为什么?这种情况最好使用1

南老頭 2012-04-23
  • 打赏
  • 举报
回复
JsonObject.toString() 自己去搞吧,
cxw3152 2012-04-23
  • 打赏
  • 举报
回复
和struts2配套的 有一个jsonPlug.jar包。。应该可以自动给你转换成JSON格式的字符。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧