Can't map a Query String parameters to my JavaBean (using Spring 4 and Datatables)

后端 未结 3 1828
名媛妹妹
名媛妹妹 2020-12-14 11:39

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\'

相关标签:
3条回答
  • 2020-12-14 12:38

    You send columns[0] as parameter, but columns has no entry with the index 0. In fact it has no entry at all as it is null.

    You have to instantiate the list in the constructor of DataTableCriterias in order to make this work. And it has to have enough entries for the parameters you send.

    0 讨论(0)
  • 2020-12-14 12:45

    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)

    0 讨论(0)
  • 2020-12-14 12:45

    I know I'm late to the party, but this: https://github.com/darrachequesne/spring-data-jpa-datatables

    is a lovely alternative to read your datatables input elegantly. Even if you dont want to use JPA, you can simply use the DataTablesInput and DataTablesOutput classes to read the input correctly. I also included jquery.spring-friendly.min.js which allowed Spring to easily read the variables that came in.

    Thanks to https://github.com/darrachequesne for a nice elegant solution.

    0 讨论(0)
提交回复
热议问题