NutzCN Logo
问答 @param 获不到取DataTablePageBean的内部类(Search)的参数
发布于 2410天前 作者 小约翰 2413 次浏览 复制 上一个帖子 下一个帖子
标签:

package com.visec.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* datatable分页工具
*/
public class DataTablePageBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
//输入必要属性
private int draw;          //dataTable必要参数
private int start = 0;       //开始的记录数
private int length = 10;       //每页显示记录数
private Search search;       //搜索栏
private List<Order> order;   //排序列表
private List<Columns> columns;  //数据属性列表

//输出必要属性
private int recordsTotal;     //总页数
private int recordsFiltered;    //过滤后总页数
private List<?> data;      //封装结果
private int dataCount;         //总记录数

//自定义属性
private Object priParam;       //私有自定义属性
private String contHql;        //统计Hql语句
private String findHql;        //查询Hql语句



public Search getSearch() {
    return search;
}

public void setSearch(Search search) {
    this.search = search;
}

public List<Order> getOrder() {
    return order;
}

public void setOrder(List<Order> order) {
    this.order = order;
}

public List<Columns> getColumns() {
    return columns;
}

public void setColumns(List<Columns> columns) {
    this.columns = columns;
}



public int getDraw() {
    return draw;
}

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

public int getRecordsTotal() {
    return recordsTotal;
}

public int getRecordsFiltered() {
    return recordsFiltered;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = (length <= 0) ? 1 : length;
}

public int getStart() {
    return start;
}

public void setStart(int start) {
    this.start = (start < 0) ? 0 : start;
}

public List<?> getData() {
    return (data != null ) ? data : new ArrayList<Object>() ;
}

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

public int getDataCount() {
    return dataCount;
}

public void setDataCount(int dataCount) {
    this.dataCount = dataCount;
    this.recordsTotal = dataCount;
    this.recordsFiltered = dataCount;
}





public Object getPriParam() {
    return priParam;
}

public void setPriParam(Object priParam) {
    this.priParam = priParam;
}

public String getContHql() {
    return contHql;
}

public void setContHql(String contHql) {
    this.contHql = contHql;
}

public String getFindHql() {
    return findHql;
}

public void setFindHql(String findHql) {
    this.findHql = findHql;
}

/**
 * 搜索内部类
 */
public static class Search implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Boolean regex;
    private String value;

    public Boolean getRegex() {
       return regex;
    }

    public void setRegex(Boolean regex) {
       this.regex = regex;
    }

    public String getValue() {
       return value;
    }

    public void setValue(String value) {
       this.value = value;
    }
}

/**
 * 排序类
 */
public static class Order implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer column;
    private String dir;

    public Integer getColumn() {
       return column;
    }

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

    public String getDir() {
       return dir;
    }

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

/**
 * 列表类
 */
public static class Columns implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String data;
    private String name;
    private Boolean orderable;
    private Boolean searchable;

    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 getOrderable() {
       return orderable;
    }

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

    public Boolean getSearchable() {
       return searchable;
    }

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



public String countHql(DataTablePageBean pageBean){
    if( pageBean.getSearch()!=null && !pageBean.getSearch().getValue().equals("")){
       return (contHql.contains("where"))
                 ? contHql+" and like %"+pageBean.getSearch().getValue()+"%"
                 : contHql+"where like %"+pageBean.getSearch().getValue()+"%";
    }
    return contHql;
}

public String findHql(DataTablePageBean pageBean){
    if(pageBean.getColumns()!=null){

       Integer columnNum = pageBean.getOrder().get(0).getColumn();
       String order = pageBean.getOrder().get(0).getDir();
       String fieldName = pageBean.getColumns().get(columnNum).getName();

       String fHql = countHql(pageBean)+" ORDER BY "+fieldName+" "+order;

       return fHql;
    }
    return findHql;
}

}

以上是DataTablePageBean对象

@At
	public Object fetchUserList(@Param("..") DataTablePageBean pageBean){
		userService.fetchUserList(pageBean);
		return ajaxOk(pageBean);
	}

这样可以获取到DataTablePageBean, 但是获取不到Search,报空指针异常

24 回复

.. 是浅层的, 换成深层的:: 试试

@Param("::")

@wendal 试了,还是空指针异常

是不是search就没数据


search: regex:false value:""

这是前端输出的数据

那就是没数据啊

贴报错信息看看

2018-03-26 16:26:54,905 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN  - Error@/user/fetchUserList :
java.lang.NullPointerException
	at com.visec.service.UserService.fetchUserList(UserService.java:57)
	at com.visec.module.UserModule.fetchUserList(UserModule.java:67)
	at com.visec.module.UserModule$FM$fetchUserList$eb7f5807d244923c1c20be7a47a8885a.invoke(UserModule.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.integration.shiro.NutShiroProcessor.process(NutShiroProcessor.java:126)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.visec.mvc.LogTimeProcessor.process(LogTimeProcessor.java:19)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)
public void  fetchUserList(DataTablePageBean pageBean) {
		System.out.println("___-"+pageBean.getSearch().getRegex() );
		System.out.println("___-"+pageBean.getSearch().getValue() );
		
		int count;
		List<User> msg;
		if(pageBean!=null && !pageBean.getSearch().getValue().equals("")){

			Cnd cnd = Cnd.where("name", "like", "%"+pageBean.getSearch().getValue()+"%");

			Pager pager = dao().createPager(pageBean.getStart(), pageBean.getLength());
		    msg = dao().fetchLinks(dao().query(User.class, cnd, pager), null);
		    count = dao().count(User.class, cnd);
		}else {

			Pager pager = dao().createPager(pageBean.getStart(), pageBean.getLength());
		    msg = dao().fetchLinks(dao().query(User.class, null, pager), null);
		    count = dao().count(User.class);
		}

	    pageBean.setData(msg);
		pageBean.setDataCount(count);
	}

at com.visec.service.UserService.fetchUserList(UserService.java:57)

57行是什么代码?

System.out.println("___-"+pageBean.getSearch().getRegex() );

pageBean.getSearch() == null 咯, 判断一下呗

没错,就是pageBean.getSearch() == null,就是@Param获取不到DataTablePageBean里面的Search内部类

@RequestMapping("/listuser")
	public void listBusinessTask(@RequestBody DataTablePageBean pageBean, HttpServletResponse response) {
		//service.listPage("select count(*) from BUserManager", "from BUserManager", pageBean, null);
		service.listUser(pageBean);
		ajaxResponse(response, JSONObject.toJSON(pageBean).toString());
	}

在SSH框架中,用@RequestBody注解可以获取Search 内部类

但Search就没数据吧?

@At
	@AdaptBy(type=JsonAdaptor.class)
	public Object fetchUserList(@Param("..") DataTablePageBean pageBean){
		userService.fetchUserList(pageBean);
		return ajaxOk(pageBean);
	}

兽总,解决了,加了个适配器就行了@AdaptBy(type=JsonAdaptor.class)

@wendal 谢谢您的回复,麻烦了

... 原来你发json数据...

只要是json数据都要加@AdaptBy(type=JsonAdaptor.class)这个适配器嘛?

添加回复
请先登陆
回到顶部