Jqgrid spring日期绑定异常
操作系统:Windows Vista,框架:Jqgrid(最新)、Spring(最新)、JQuery(最新) 我正在使用Jqgrid将表单发布到Spring控制器以进行持久化。当Spring控制器尝试将请求参数自动绑定到域对象时,它在尝试绑定“日期”数据类型时抛出异常。我使用JSon格式传输数据。Jqgrid正确显示日期。传输字符串包含“&-quot;”导致异常的日期前后的字符。我不知道如何从Jqgrid中删除转义字符。我不知道如何在Spring有机会自动绑定之前拦截字符串。提前谢谢你的帮助Jqgrid spring日期绑定异常,spring,jqgrid,Spring,Jqgrid,操作系统:Windows Vista,框架:Jqgrid(最新)、Spring(最新)、JQuery(最新) 我正在使用Jqgrid将表单发布到Spring控制器以进行持久化。当Spring控制器尝试将请求参数自动绑定到域对象时,它在尝试绑定“日期”数据类型时抛出异常。我使用JSon格式传输数据。Jqgrid正确显示日期。传输字符串包含“&-quot;”导致异常的日期前后的字符。我不知道如何从Jqgrid中删除转义字符。我不知道如何在Spring有机会自动绑定之前拦截字符串。提前谢谢你的帮助
public class JsonDateSerializer extends JsonSerializer<Date> {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
AtOverride
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
您可能可以使用StringTrimmerEditor的第二个构造函数来删除这些额外的字符
StringTrimmerEditor(String charsToDelete, boolean emptyAsNull)
根据Spring文档:
charsToDelete-除了修剪输入字符串之外,还要删除的一组字符。用于删除不需要的换行符。例如“\r\n\f”将删除字符串中的所有新行和换行符
我还使用了最新版本的JqGrid和Spring3,但我处理参数的方式似乎更简单。我是这样做的:
@Controller
@RequestMapping("/json")
public class JsonController {
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody JsonResponse getAll(
@RequestParam("_search") String search,
@RequestParam(value="filters", required=false) String filters,
@RequestParam(value="datefrom", required=false) String datefrom,
@RequestParam(value="dateto", required=false) String dateto,
@RequestParam(value="page", required=false) String page,
@RequestParam(value="rows", required=false) String rows,
@RequestParam(value="sidx", required=false) String sidx,
@RequestParam(value="sord", required=false) String sord
) { ... }
所有参数都作为普通字符串传递。datefrom和dateto是我从JqGrid传递的自定义参数。日期来自JQuery的日期选择器,并通过JqGrid postData传递:
postData: {
datefrom: function() { return jq("#datepicker_from").datepicker("getDate"); },
dateto: function() { return jq("#datepicker_to").datepicker("getDate"); }
},
JsonResponse是一个简单的POJO,我用来映射JqGrid传递的搜索参数:
public class JsonResponse {
/**
* Current page of the query
*/
private String page;
/**
* Total pages for the query
*/
private String total;
/**
* Total number of records for the query
*/
private String records;
/**
* An array that contains the actual data
*/
private List<MyDTO> rows;
public JsonResponse() {
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getRecords() {
return records;
}
public void setRecords(String records) {
this.records = records;
}
public List<MyDTO> getRows() {
return rows;
}
public void setRows(List<MyDTO> rows) {
this.rows = rows;
}
公共类JsonResponse{
/**
*查询的当前页面
*/
私有字符串页;
/**
*查询的总页数
*/
私有字符串总数;
/**
*查询的记录总数
*/
私有字符串记录;
/**
*包含实际数据的数组
*/
私有列表行;
公共JsonResponse(){
}
公共字符串getPage(){
返回页面;
}
公共无效设置页(字符串页){
this.page=page;
}
公共字符串getTotal(){
返回总数;
}
公共void集合总计(字符串总计){
这个.总计=总计;
}
公共字符串getRecords(){
退货记录;
}
公共无效集合记录(字符串记录){
这个.记录=记录;
}
公共列表getRows(){
返回行;
}
公共无效集合行(列表行){
this.rows=行;
}
}
MyDTO也是一个简单的DTO对象
在我的Spring applicationContext.xml中,我只需声明以下内容:
<mvc:annotation-driven/>
<mvc:annotation-driven/>
并添加了jacksonjar,用于从JSON转换为POJO,反之亦然
为了将字符串日期转换为真实日期,我使用了伟大的Joda库。当然,您可以使用JDK的标准日期。您可能可以使用StringTrimmerEditor的第二个构造函数来删除这些额外的字符
StringTrimmerEditor(String charsToDelete, boolean emptyAsNull)
根据Spring文档:
charsToDelete-除了修剪输入字符串之外,还要删除的一组字符。用于删除不需要的换行符。例如“\r\n\f”将删除字符串中的所有新行和换行符
我还使用了最新版本的JqGrid和Spring3,但我处理参数的方式似乎更简单。我是这样做的:
@Controller
@RequestMapping("/json")
public class JsonController {
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody JsonResponse getAll(
@RequestParam("_search") String search,
@RequestParam(value="filters", required=false) String filters,
@RequestParam(value="datefrom", required=false) String datefrom,
@RequestParam(value="dateto", required=false) String dateto,
@RequestParam(value="page", required=false) String page,
@RequestParam(value="rows", required=false) String rows,
@RequestParam(value="sidx", required=false) String sidx,
@RequestParam(value="sord", required=false) String sord
) { ... }
所有参数都作为普通字符串传递。datefrom和dateto是我从JqGrid传递的自定义参数。日期来自JQuery的日期选择器,并通过JqGrid postData传递:
postData: {
datefrom: function() { return jq("#datepicker_from").datepicker("getDate"); },
dateto: function() { return jq("#datepicker_to").datepicker("getDate"); }
},
JsonResponse是一个简单的POJO,我用来映射JqGrid传递的搜索参数:
public class JsonResponse {
/**
* Current page of the query
*/
private String page;
/**
* Total pages for the query
*/
private String total;
/**
* Total number of records for the query
*/
private String records;
/**
* An array that contains the actual data
*/
private List<MyDTO> rows;
public JsonResponse() {
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getRecords() {
return records;
}
public void setRecords(String records) {
this.records = records;
}
public List<MyDTO> getRows() {
return rows;
}
public void setRows(List<MyDTO> rows) {
this.rows = rows;
}
公共类JsonResponse{
/**
*查询的当前页面
*/
私有字符串页;
/**
*查询的总页数
*/
私有字符串总数;
/**
*查询的记录总数
*/
私有字符串记录;
/**
*包含实际数据的数组
*/
私有列表行;
公共JsonResponse(){
}
公共字符串getPage(){
返回页面;
}
公共无效设置页(字符串页){
this.page=page;
}
公共字符串getTotal(){
返回总数;
}
公共void集合总计(字符串总计){
这个.总计=总计;
}
公共字符串getRecords(){
退货记录;
}
公共无效集合记录(字符串记录){
这个.记录=记录;
}
公共列表getRows(){
返回行;
}
公共无效集合行(列表行){
this.rows=行;
}
}
MyDTO也是一个简单的DTO对象
在我的Spring applicationContext.xml中,我只需声明以下内容:
<mvc:annotation-driven/>
<mvc:annotation-driven/>
并添加了jacksonjar,用于从JSON转换为POJO,反之亦然
为了将字符串日期转换为真实日期,我使用了伟大的Joda库。当然,你可以使用JDK的标准日期。如果我今天有时间,我会尝试在我的博客上设置一个教程 我的JSP文件只是一个简单的JSP文件,带有典型的JqGrid声明:
<script type="text/javascript">
jq(function() {
// This is the grid
jq("#grid").jqGrid({
url:'/myapp/users',
datatype: 'json',
mtype: 'GET',
colNames:['Id','Username','First Name'],
colModel:[
{name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},
{name:'firstName',index:'firstName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}, editrules:{required:true}},
{name:'lastName',index:'lastName', width:80, align:"right",editable:true, editrules:{required:true}, editoptions:{size:10}, editrules:{required:true}}
],
postData: {
// Here you can post extra parameters
// For example using JQuery you can retrieve values of other css elements
},
rowNum:10,
rowList:[10,20,30],
height: 200,
autowidth: true,
rownumbers: true,
pager: '#pager',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
caption:"Users",
emptyrecords: "Empty records",
loadonce: false,
loadComplete: function() {
// Here you can provide extra functions after the grid is loaded completely
// Like auto-height function
},
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
cell: "cell",
id: "id"
}
});
// This is the pager
jq("#grid").jqGrid('navGrid','#pager',
{edit:false,add:false,del:false,search:true},
{ },
{ },
{ },
{
sopt:['eq', 'ne', 'lt', 'gt', 'cn', 'bw', 'ew'],
closeOnEscape: true,
multipleSearch: true,
closeAfterSearch: true }
);
// Custom Add button on the pager
jq("#grid").navButtonAdd('#pager',
{ caption:"Add",
buttonicon:"ui-icon-plus",
onClickButton: addRow,
position: "last",
title:"",
cursor: "pointer"
}
);
// Custom Edit button on the pager
jq("#grid").navButtonAdd('#pager',
{ caption:"Edit",
buttonicon:"ui-icon-pencil",
onClickButton: editRow,
position: "last",
title:"",
cursor: "pointer"
}
);
// Custom Delete button on the pager
jq("#grid").navButtonAdd('#pager',
{ caption:"Delete",
buttonicon:"ui-icon-trash",
onClickButton: deleteRow,
position: "last",
title:"",
cursor: "pointer"
}
);
// Toolbar Search
jq("#grid").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true, defaultSearch:"cn"});
});
<script type="text/javascript">
var jq = jQuery.noConflict();
</script>
function addRow() {
// Get the currently selected row
jq("#grid").jqGrid('editGridRow','new',
{ url: "/myapp/users/add",
editData: {
// Here you add extra post parameters
},
recreateForm: true,
beforeShowForm: function(form) {
// Here you can add, disable, hide elements from the popup form
},
closeAfterAdd: true,
reloadAfterSubmit:false,
afterSubmit : function(response, postdata)
{
// This is a callback function that will evaluate the response sent by your Controller
var result = eval('(' + response.responseText + ')');
var errors = "";
if (result.success == false) {
// Do whatever you like if not successful
} else {
// Do whatever you like if successful
}
// only used for adding new records
var new_id = null;
// Then this will be returned back to the popup form
return [result.success, errors, new_id];
}
});
}
jq(函数(){
//这是网格
jq(“网格”).jqGrid({
url:“/myapp/users”,
数据类型:“json”,
mtype:'获取',
colNames:['Id','Username','First Name'],
colModel:[
{name:'id',index:'id',width:55,edit:false,editoptions:{readonly:true,size:10},hidden:true},
{name:'firstName',index:'firstName',width:100,editrules:{required:true},editoptions:{size:10},editrules:{required:true},
{name:'lastName',index:'lastName',width:80,align:'right',edit:true,editrules:{required:true},editoptions:{size:10},editrules:{required:true}
],
postData:{
//在这里你可以发布额外的参数
//例如,使用JQuery可以检索其他css元素的值
},
rowNum:10,
行列表:[10,20,30],
身高:200,
自动宽度:正确,
行数:对,
寻呼机:“#寻呼机”,
sortname:'id',
viewrecords:是的,
分拣员:“asc”,
标题:“用户”,
emptyrecords:“空记录”,
loadonce:false,
loadComplete:function(){
//在这里,您可以提供额外的功能