查询框效果如下
1.前端代码(有其他的业务代码,请自行略过....):
管理分区
主要步骤是:(1)给查询按钮添加点击事件;(2).将表单序列化成json对象,指定组合条件分页查询action的url;
将表单序列化成json对象代码:
//将表单序列化为json对象 $.fn.serializeJson=function(){ var serializeObj={}; var array=this.serializeArray(); var str=this.serialize(); $(array).each(function(){ if(serializeObj[this.name]){ if($.isArray(serializeObj[this.name])){ serializeObj[this.name].push(this.value); }else{ serializeObj[this.name]=[serializeObj[this.name],this.value]; } }else{ serializeObj[this.name]=this.value; } }); return serializeObj; };
2.后台代码
action
@Controller@Scope("prototype")public class SubAreaAction extends BaseAction{ // 注入service @Autowired private SubAreaService subAreaService; /** * 组合条件+分页查询的方法 */ @Action("subarea_listPage") public String listPage() { //创建分页对象 Pageable pageable = new PageRequest(page - 1, rows); Specification spec = new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List andPredicate = new ArrayList (); List orPredicate = new ArrayList (); //先判断区域是否存在 if(model.getArea() != null ) { Join
service:
public interface SubAreaService { PagelistPage(Specification spec, Pageable pageable);}
serviceImpl:
@Service@Transactionalpublic class SubAreaServiceImpl implements SubAreaService { //注入dao @Autowired private SubAreaRepository subAreaRepository;/** * 分页查询的方法 */ @Override public PagelistPage(Specification spec,Pageable pageable) { return subAreaRepository.findAll(spec, pageable); } }
dao层:
public interface SubAreaRepository extends JpaRepository,JpaSpecificationExecutor { }