本文介绍了无法将查询字符串参数映射到我的JavaBean(使用Spring 4和Datatables)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的坚持尝试将我的QueryString参数映射到我的Spring JavaBean Command对象中,到目前为止我找不到我的问题的答案。



我正在使用的jQuery Datatables插件,以便我的datatable中的每个操作,触发对我的Spring应用程序的AJAX请求。



这是Datatable插件发送到我的休息服务的参数:

  http:// localhost:8080 / relatorios / produtos-source?draw = 2& columns [0] [data] = nome& columns [0] [name] =&列[0] [searchable] = true& columns [0] [orderable] = true& columns [0] [search] [value] =& columns [0] [search] [regex] = false& [data] = nomesAlternativos& columns [1] [name] =& columns [1] [searchable] = true& columns [1] [orderable] = true& columns [1] [search] [value] =& columns [1] [search] [regex] = false& order [0] [column] = 2& order [0] [dir] = asc & start = 0& length = 10& search [value] = ss& search [regex] = false& _ = 1400248561282 

这是我在Spring Controller中收到的:

  @RequestMapping(value =/ produtos -source,method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criteria){
...

最后,这是我的DataTableCriterias JavaBean(由@ModelAttribute指示):

  public class DataTableCriterias {
private int draw;
private int start;
private int length;

private Map< SearchCriterias,String>搜索;

private List< Map< OrderCriterias,String>>订购;

私人列表<列>列;

public enum SearchCriterias {
value,
regex
}

public enum OrderCriterias {
column,
dir
}

public class Column {
private String data;
private String name;
private boolean searchable;
private boolean orderable;
private Map< SearchCriterias,String>搜索;
}
}

(get / setters ommited) em>



这几乎可以完美地工作!如果我删除这一行:

 私人列表<列>列;然后,Spring从查询字符串中自动填充我的DataTableCriterias bean(但是当然,我不会得到列属性映射)



但是添加了这一行,我收到这个错误:

 code> 2014-05-16 17:20:16.605错误2368 --- [tomcat-http  -  99] oaccC [。[。[dispatcherServlet]:Servlet.service()for servlet [dispatcherServlet] in路径[/ relatorios]的上下文抛出异常[请求处理失败;嵌套异常是org.springframework.beans.InvalidPropertyException:bean类[com.bergermobile.rest.domain.DataTableCriterias]的无效属性列[0] [data]':非法尝试获取属性列抛出异常;嵌套异常是org.springframework.beans.NullValueInNestedPathException:bean类[com.bergermobile.rest.domain.DataTableCriterias]的属性列无效:无法将属性类型[com.bergermobile.rest.domain.DataTableCriterias $ Column]实例化为自动增长嵌套的属性路径:java.lang.InstantiationException:com.bergermobile.rest.domain.DataTableCriterias $ Column]与根本原因

org.springframework.beans.NullValueInNestedPathException:无效的属性列 bean类[com.bergermobile.rest.domain.DataTableCriterias]:无法实例化属性类型[com.bergermobile.rest.domain.DataTableCriterias $ Column]以自动增长嵌套属性路径:java.lang.InstantiationException:com.bergermobile。 rest.domain.DataTableCriterias $ column
在org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:651)

正如我所看到的,主要的问题是Datatable发送的'column'参数插件是一个二维的,有时也是一个三维数组:

  columns [0] [data] = nome 
columns [ 0] [search] [regex] = false

所以,看起来我的Bean是正确的我收到这个错误,现在我被卡住了。



任何想法?



非常感谢! p>

解决方案

好的,所以我最后修改了这个问题,稍微修改发送到服务器的参数,以转换两个3D列数组变成2D阵列。所以:

 列[0] [搜索] [价值] = myvalue 
列[0] [搜索] regex] = false

最终成为:

 列[0] [searchValue] = myvalue 
列[0] [searchRegex] = false

这是如何在数据库中进行的:

  $('#produtosTable ').DataTable({
serverSide:true,
ajax:{
url:produtos-source,
data:function(data){
planify(data);
}
}
});

函数planify(data){
for(var i = 0; i< data.columns.length; i ++){
column = data.columns [i];
column.searchRegex = column.search.regex;
column.searchValue = column.search.value;
delete(column.search);
}
}

这样我就可以在我的Model对象中收到这些属性使用此字段:

 私人列表< Map< ColumnCriterias,String>>列; 






仅供参考,这里是我的控制器: / p>

  @RequestMapping(value =/ produtos-source,method = RequestMethod.GET)
@ResponseStatus(HttpStatus。 OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criteria){
ProdutoTable produtoTable = produtosService.findProdutos();
produtoTable.setDraw(standardsias.getDraw());
return produtoTable;
}

这里是最终的我的DataTableCriterias @ModelAttriute:

  public class DataTableCriterias {
private int draw;
private int start;
private int length;

private Map< SearchCriterias,String>搜索;

private List< Map< ColumnCriterias,String>>列;

private List< Map< OrderCriterias,String>>订购;

public enum SearchCriterias {
value,
regex
}
public enum OrderCriterias {
column,
dir
}
public enum ColumnCriterias {
data,
name,
可搜索,
可订购,
searchValue,
searchRegex
}

(get / setters省略)


I'm really stuck into trying to map my QueryString parameters into my Spring JavaBean Command object here, and I couldn't find an answer to my question so far.

I'm using jQuery Datatables plugin with server side processing so that each action in my datatable, triggers an AJAX request to my Spring application.

This is the parameters the Datatable plugin is sending to my Rest service:

http://localhost:8080/relatorios/produtos-source?draw=2&columns[0][data]=nome&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false&columns[1][data]=nomesAlternativos&columns[1][name]=&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&order[0][column]=2&order[0][dir]=asc&start=0&length=10&search[value]=ss&search[regex]=false&_=1400248561282

This is how I am receiving it in my Spring Controller:

@RequestMapping(value = "/produtos-source", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criterias) {
    ...
}

And finally, this is my DataTableCriterias JavaBean (indicated by @ModelAttribute):

public class DataTableCriterias {
    private int draw;
    private int start;
    private int length;

    private Map<SearchCriterias, String> search;

    private List<Map<OrderCriterias, String>> order;

    private List<Column> columns;

    public enum SearchCriterias {
        value,
        regex
    }

    public enum OrderCriterias {
        column,
        dir
    }

    public class Column {
        private String data;
        private String name;
        private boolean searchable;
        private boolean orderable;
        private Map<SearchCriterias, String> search;
    }
}

(get / setters ommited)

This almost works perfectly! If I remove this line:

private List<Column> columns;

then Spring automagically populates my DataTableCriterias bean from the query string (but of course, I don't get the columns property mapped)

But with this line added, I get this error:

2014-05-16 17:20:16.605 ERROR 2368 --- [tomcat-http--99] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/relatorios] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'columns[0][data]' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Illegal attempt to get property 'columns' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'columns' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Could not instantiate property type [com.bergermobile.rest.domain.DataTableCriterias$Column] to auto-grow nested property path: java.lang.InstantiationException: com.bergermobile.rest.domain.DataTableCriterias$Column] with root cause

org.springframework.beans.NullValueInNestedPathException: Invalid property 'columns' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Could not instantiate property type [com.bergermobile.rest.domain.DataTableCriterias$Column] to auto-grow nested property path: java.lang.InstantiationException: com.bergermobile.rest.domain.DataTableCriterias$Column
    at org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:651)

As I see it, the main problem is that the 'column' parameter sent by the Datatable plugin is a bidimensional and sometimes also a tridimensional array:

columns[0][data]=nome
columns[0][search][regex]=false

So, it looks like my Bean was correct, but I get this error and now I'm stuck.

Any ideas?

Thanks a lot!

解决方案

Ok, so I ended up fixing this issue with slightly modifying the parameters sent to the server, to transform the two 3D array of columns into a 2D array. So:

columns[0][search][value]=myvalue
columns[0][search][regex]=false

ended up being:

columns[0][searchValue]=myvalue
columns[0][searchRegex]=false

This is how to do that in Databables:

$('#produtosTable').DataTable({
    serverSide: true,
    ajax: {
        "url": "produtos-source",
        "data": function(data) {
            planify(data);
        }
    }
});

function planify(data) {
    for (var i = 0; i < data.columns.length; i++) {
        column = data.columns[i];
        column.searchRegex = column.search.regex;
        column.searchValue = column.search.value;
        delete(column.search);
    }
}

This way I can receive those properties in my Model object using this field:

private List<Map<ColumnCriterias, String>> columns;


Just for reference, here is my Controller:

@RequestMapping(value = "/produtos-source", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criterias) {
    ProdutoTable produtoTable = produtosService.findProdutos();
    produtoTable.setDraw(criterias.getDraw());
    return produtoTable;
}

and here is final my DataTableCriterias @ModelAttriute:

public class DataTableCriterias {
    private int draw;
    private int start;
    private int length;

    private Map<SearchCriterias, String> search;

    private List<Map<ColumnCriterias, String>> columns;

    private List<Map<OrderCriterias, String>> order;

    public enum SearchCriterias {
        value,
        regex
    }
    public enum OrderCriterias {
        column,
        dir
    }
    public enum ColumnCriterias {
        data,
        name,
        searchable,
        orderable,
        searchValue,
        searchRegex
    }

(get/setters omitted)

这篇关于无法将查询字符串参数映射到我的JavaBean(使用Spring 4和Datatables)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:15