学习了框架之后 ,某天用ssh框架在做练手demo遇到一个很奇葩的问题: 一个记录表(TradeRecord)一个类型表(TradeType) 他们 单向多对一 我根据类型条件查询的时候遇到图上问题。
我的请求代码:
$(function(){
$('#search').click(function(){
$("#tbodyId").html("");
var type=$('#type').val();
var starDate=$('#starDate').val();
var endDate=$('#endDate').val();
$.ajax({
url:'findParame',
data:{
type:type,
starDate:starDate,
endDate:endDate
},
type:"post",
dataType:"json",
success:function(data)
{
var json=eval(data);
var html=$("#tbodyId").html();
$.each(json,function(){
html+="<tr> <td>"+this.recordTime+"</td> <td>"+this.recordNo+"</td> <td>"+this.rocordMoney+"</td> <td>"+this.rocordMark+"</td><td>"+this.type.tradeName+"</td> </tr>";
})
$("#tbodyId").html(html);
}
});
});
});
daoimpl对应代码:
@SuppressWarnings("unchecked")
public List<TradeRecord> findByParames(String type,String starDate,String endDate)
{
String sql="select * from traderecord r INNER JOIN tradetype t on r.TRADETYPE_ID=t.TRADETYPE_ID where 1=1 ";
if(type!=null)
{
sql+=" and t.TRADETYPE_ID="+type;
}
if(!starDate.equals("--请选择--")&&!endDate.equals("--请选择--"))
{
sql+=" and r.TRADERECORD_Time BETWEEN '"+starDate+"' and '"+endDate+"'";
}
final String Sql=sql;
List<TradeRecord>list=this.getHibernateTemplate().executeFind
(
new HibernateCallback<List<TradeRecord>>()
{
public List<TradeRecord> doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery query=session.createSQLQuery(Sql);
return query.addEntity(TradeRecord.class).list();
}
}
);
return list;
}
service 代码:
public List<TradeRecord> findByParames(String type,String starDate,String endDate)
{
return dao.findByParames(type, starDate, endDate);
}
action 代码:
public void findByParames() throws UnsupportedEncodingException
{
HttpServletRequest request= ServletActionContext.getRequest();
request.setCharacterEncoding("utf-8");
PrintWriter out=null;
String type=request.getParameter("type");
String starDate=request.getParameter("starDate");
String endDate=request.getParameter("endDate");
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
List<TradeRecord> list=svr.findByParames(type, starDate, endDate);
try {
out=ServletActionContext.getResponse().getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSON.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";
String jsonText = JSON.toJSONString(list,SerializerFeature.WriteDateUseDateFormat);
System.out.println(jsonText);
out.write(jsonText);
out.flush();
out.close();
}
我先后用调试看了下list的值 和jsonText的值 终于发现了问题:问题就在String jsonText = JSON.toJSONString(list,SerializerFeature.WriteDateUseDateFormat);
我打印下jsonText:
[{"recordNo":121,"recordTime":"2016-01-16","rocordMark":"买手机","rocordMoney":1280.8,"type":{"tradeId":32,"tradeName":"提现"}},{"recordNo":123,"recordTime":"2016-01-16","rocordMark":"缴纳电费","rocordMoney":128,"type":{"$ref":"$[0].type"}},{"recordNo":124,"recordTime":"2016-01-16","rocordMark":"买平板电脑","rocordMoney":996.9,"type":{"$ref":"$[0].type"}},{"recordNo":126,"recordTime":"2016-01-16","rocordMark":"话费充值","rocordMoney":500,"type":{"$ref":"$[0].type"}},{"recordNo":128,"recordTime":"2016-01-16","rocordMark":"11月工资到帐","rocordMoney":6000,"type":{"$ref":"$[0].type"}}]
我去查了下api 终于明白了原因:当进行toJSONString的时候,默认如果重用对象的话,会使用引用的方式进行引用对象。
解决方法只需要将toJSONString 改成
String jsonText = JSON.toJSONString(list,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect);
启动就好了
关于时间的处理:API上面有几种处理方案 我就写了我用着行的方案:
JSON.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";
String jsonText = JSON.toJSONString(list,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect);