package com.newland.honeyweek.json.action; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import net.sf.json.JSONObject; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.json.annotations.JSON; import com.newland.honeyweek.common.PageResultSet; import com.newland.honeyweek.common.Pager; import com.newland.honeyweek.model.Honey; import com.newland.honeyweek.model.Letter; import com.newland.honeyweek.service.LetterService; import com.newland.honeyweek.service.PhotoService; import com.newland.honeyweek.service.UserService; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") @ParentPackage("json-default") @Namespace("/") @Action("jsonAction") @Results(value = { @Result(name = "test",type="json") }) public class JsonAction extends ActionSupport implements Serializable { @Resource private UserService userService; @Resource private LetterService letterService; @Resource private PhotoService photoService; private Pager pager = new Pager(); private Letter letter; private PageResultSet<Honey> pageResultSet; private PageResultSet<Letter> letterPage; private String jsonResult; @Override public String execute() throws Exception { return super.execute(); } public String inBoxTest() { String hql = "From Letter letter where letter.toWho='2@qq.com' group by letter.toWho order by sendTime desc"; letterPage = letterService.queryByPage(hql, 12, pager.getOffset()); List<Letter> list=letterPage.getRows(); PageResultSet<Letter> ret=new PageResultSet<Letter>(); ret.setRows(list); ret.setRowCount(list.size()); JSONObject jsonObject = JSONObject.fromObject(ret); jsonResult=jsonObject .toString(); System.out.println(jsonResult); return "test"; } //以下三个getter and setter方法导致了本次的错误 public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public LetterService getLetterService() { return letterService; } public void setLetterService(LetterService letterService) { this.letterService = letterService; } public PhotoService getPhotoService() { return photoService; } public void setPhotoService(PhotoService photoService) { this.photoService = photoService; } public Pager getPager() { return pager; } public void setPager(Pager pager) { this.pager = pager; } public Letter getLetter() { return letter; } public void setLetter(Letter letter) { this.letter = letter; } public PageResultSet<Honey> getPageResultSet() { return pageResultSet; } public void setPageResultSet(PageResultSet<Honey> pageResultSet) { this.pageResultSet = pageResultSet; } public PageResultSet<Letter> getLetterPage() { return letterPage; } public void setLetterPage(PageResultSet<Letter> letterPage) { this.letterPage = letterPage; } @JSON(serialize = false) public String getJsonResult() { return jsonResult; } public void setJsonResult(String jsonResult) { this.jsonResult = jsonResult; } }
$(function() { $('#hrUser_datagrid').datagrid({ url : '<%=request.getContextPath()%>/jsonAction!inBoxTest.action', fit : true, fitColumns : true, border : false, pagination : true, idField : 'id', pageSize : 10, pageList : [ 10, 20, 30, 40, 50 ], checkOnSelect : false, selectOnCheck : false, onClickRow : function(value,row,index){ HrUser.initHrUserGrid(row.userId,subSysCode); }, nowrap : false, frozenColumns : [ [ {title : '信件编码',field : 'id',width : 150,checkbox : true}] ], columns : [ [ {title : '发件人',field : 'fromWho',width : 150}, {title : '收件人',field : 'toWho',width : 150}, {title : '发送时间',field : 'sendTime',width : 150}, {title : '接收时间',field : 'receiveTime',width : 150}, {title : '状态',field : 'statue',width : 150}, {title : '消息内容',field : 'message',width : 150} ] ], toolbar :'#toolbar' }); }); 纠其原因:是因为struts2的json控件把service类当成属性对象返回了