注意:所有文章除特别说明外,转载请注明出处.
概念
自定义HandlerMethodArgumentResolver可以理解为用一个对象封装需要查询的参数到后台查询相关值。
1.自定义注解
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PropertyNaming {
}
2.实现方法
package cn.edu.xidian.see.ext;
//这里省略导入的包文件
...
//在编写之前应该实现此解决入参问题的接口
public class DataTableArgumentResolver implements HandlerMethodArgumentResolver {
//1.首先需要实现的方法是:supportsParameter()方法
@Override
public boolean supportsParameter(MethodParameter parameter) {
// 仅作用于添加了注解DataTableArgument的参数
return parameter.hasParameterAnnotation(DataTableArgument.class);
}
//2.然后重写resolveArgument方法
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
DataTableRequest dtRequest = new DataTableRequest();
if (webRequest.getParameter("draw") != null) {
dtRequest.setDraw(Integer.valueOf(webRequest.getParameter("draw")));
}
if (webRequest.getParameter("start") != null) {
dtRequest.setStart(Integer.valueOf(webRequest.getParameter("start")));
}
if (webRequest.getParameter("length") != null) {
dtRequest.setLength(Integer.valueOf(webRequest.getParameter("length")));
}
Map<String, String[]> parameterMap = webRequest.getParameterMap();
if (parameterMap.containsKey("search[value]")) {
Search search = new Search();
search.setValue(parameterMap.get("search[value]")[0]);
search.setRegex(Boolean.valueOf(parameterMap.get("search[regex]")[0]));
dtRequest.setSearch(search);
}
List<Order> orders = new ArrayList<>();
int i = 0;
String ockey = "order[" + i + "][column]", odkey;
while (parameterMap.containsKey(ockey)) {
odkey = "order[" + i + "][dir]";
Order order = new Order();
order.setColumn(Integer.valueOf(parameterMap.get(ockey)[0]));
order.setDir(parameterMap.get(odkey)[0]);
orders.add(order);
ockey = "order[" + (++i) + "][column]";
}
dtRequest.setOrder(orders);
List<Column> columns = new ArrayList<>();
i = 0;
String cdkey, cnkey, cskey, cokey, csvkey, csrkey;
cdkey = "columns[" + i + "][data]";
while (parameterMap.containsKey(cdkey)) {
cnkey = "columns[" + i + "][name]";
cskey = "columns[" + i + "][searchable]";
cokey = "columns[" + i + "][orderable]";
csvkey = "columns[" + i + "][search][value]";
csrkey = "columns[" + i + "][search][regex]";
Column column = new Column();
column.setData(parameterMap.get(cdkey)[0]);
column.setName(parameterMap.get(cnkey)[0]);
column.setSearchable(Boolean.valueOf(parameterMap.get(cskey)[0]));
column.setOrderable(Boolean.valueOf(parameterMap.get(cokey)[0]));
Search search = new Search();
search.setValue(parameterMap.get(csvkey)[0]);
search.setRegex(Boolean.valueOf(parameterMap.get(csrkey)[0]));
column.setSearch(search);
columns.add(column);
cdkey = "columns[" + (++i) + "][data]";
}
dtRequest.setColumns(columns);
return dtRequest;
}
}
3.使用方法
@RequestMapping(value = "/teacher/process", produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String teacher_process(HttpSession session,
@DataTableArgument DataTableRequest dtRequest,
@RequestParam(value = "showColumns[]", required = false) String[] showColumns) throws Exception{
//controller程序实现
}