用于x-www-form-urlencoded类型请求的Spring bean

时间:2015-09-28 11:45:57

标签: java jquery spring

我需要在后端使用spring实现jquery datatables服务器端处理。我有x-www-form-urlencoded类型的帖子请求:

draw:1
columns[0][data]:id
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:name
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
order[0][column]:0
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false

我的问题是什么样的java bean对应这个请求?

1 个答案:

答案 0 :(得分:1)

Spring pojo绑定有时可能会有点困难,在那些没有多少选择的情况下,只需用HttpServletRequest自己映射值。

我也必须使用datatables并创建一个AjaxRequest类:

public class AjaxRequest implements Serializable {

    public static class Column {

        private int index;

        private String data;

        private String name;

        private boolean searchable;

        private boolean orderable;

        private String searchValue;

        private boolean searchRegex;

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isSearchable() {
            return searchable;
        }

        public void setSearchable(boolean searchable) {
            this.searchable = searchable;
        }

        public boolean isOrderable() {
            return orderable;
        }

        public void setOrderable(boolean orderable) {
            this.orderable = orderable;
        }

        public String getSearchValue() {
            return searchValue;
        }

        public void setSearchValue(String searchValue) {
            this.searchValue = searchValue;
        }

        public boolean isSearchRegex() {
            return searchRegex;
        }

        public void setSearchRegex(boolean searchRegex) {
            this.searchRegex = searchRegex;
        }
    }

    public static class Order {

        private int index;

        private int column;

        private OrderDirection dir;

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getColumn() {
            return column;
        }

        public void setColumn(int column) {
            this.column = column;
        }

        public OrderDirection getDir() {
            return dir;
        }

        public void setDir(OrderDirection dir) {
            this.dir = dir;
        }
    }

    public enum OrderDirection {
        asc, desc;
    }

    private static final long serialVersionUID = 1L;

    private Integer draw;

    private Integer start;

    private Integer length;

    private String searchValue;

    private boolean searchRegex;

    private List<Column> columns = new LinkedList<>();

    private List<Order> orders = new LinkedList<>();

    public AjaxRequest() {

    }

    public Integer getDraw() {
        return draw;
    }

    public void setDraw(Integer draw) {
        this.draw = draw;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public boolean isSearchRegex() {
        return searchRegex;
    }

    public void setSearchRegex(boolean searchRegex) {
        this.searchRegex = searchRegex;
    }

    public List<Column> _getColumns() {
        return columns;
    }

    public List<Order> _getOrders() {
        return orders;
    }

    public void bind(HttpServletRequest request) {
        String searchRegexParam = request.getParameter("search[regex]");

        this.searchValue = request.getParameter("search[value]");

        if (StringUtils.isNotBlank(searchRegexParam)) {
            this.searchRegex = searchRegexParam.equals("true");
        }

        for (int i = 0;; i++) {
            String cDataParam = request.getParameter("columns[" + i + "][data]");
            String cNameParam = request.getParameter("columns[" + i + "][name]");
            String cSearchableParam = request.getParameter("columns[" + i + "][searchable]");
            String cOrderableParam = request.getParameter("columns[" + i + "][orderable]");
            String cSearchValueParam = request.getParameter("columns[" + i + "][search][value]");
            String cSearchRegexParam = request.getParameter("columns[" + i + "][search][regex]");

            // No more columns
            if (cDataParam == null) {
                break;
            }

            Column column = new Column();
            column.setIndex(i);
            column.setData(cDataParam);
            column.setName(cNameParam);
            column.setSearchable(StringUtils.equals(cSearchableParam, "true"));
            column.setOrderable(StringUtils.equals(cOrderableParam, "true"));
            column.setSearchValue(cSearchValueParam);
            column.setSearchRegex(StringUtils.equals(cSearchRegexParam, "true"));

            this.columns.add(column);
        }

        for (int i = 0;; ++i) {
            String orderColumnIdx = request.getParameter("order[" + i + "][column]");

            if (StringUtils.isEmpty(orderColumnIdx)) {
                break;
            }

            String orderDirection = request.getParameter("order[" + i + "][dir]");

            if (StringUtils.isEmpty(orderDirection)) {
                break;
            }

            Order order = new Order();
            order.setIndex(i);
            order.setColumn(Integer.valueOf(orderColumnIdx));
            order.setDir(OrderDirection.valueOf(orderDirection));

            this.orders.add(order);
        }
    }
}

要映射的难点是columnsorders所以我创建了内部类来处理它们。要使用它,只需:

@RequestMapping(produces = "application/json")
@ResponseBody
public AjaxResponse ajax(HttpServletRequest request, AjaxRequest ajaxRequest) {
    ajaxRequest.bind(request);
    // ...
}

春天无法映射的所有属性都将由bind方法处理。

这是我正在使用的AjaxResponse

@JsonInclude(value = Include.NON_EMPTY)
public class AjaxResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    private long draw;

    private long recordsTotal;

    private long recordsFiltered;

    private List<Object> data = new ArrayList<>();

    private String error;

    public AjaxResponse() {

    }

    public AjaxResponse(int draw) {
        this.draw = draw;
    }

    public long getDraw() {
        return draw;
    }

    public void setDraw(long draw) {
        this.draw = draw;
    }

    public long getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public long getRecordsFiltered() {
        return recordsFiltered;
    }

    public void setRecordsFiltered(long recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    public List<Object> getData() {
        return data;
    }

    public void setData(List<Object> data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}

对于那些想知道我必须编写此代码以便与https://www.datatables.net

有效合作的人