Spring MVC Federated Query + pagination + Conditional search + page Local Refresh

Source: Internet
Author: User
Tags foreach first row stringbuffer

Spring Mvc+hibernate implements background data query paging

jquery+jsp Implement page Local Refresh

The frame has been set.

First, data joint query

The table structure of the federated query should be similar, and the query results are stored using the table aliases and the federated information Tables of the configuration

1. Defining entity tables for federated Queries Unionstudent.java

Class unionstudent{

private String name;

Private String sex;

Getter ():

Setter ():

}

2. Define the paging class Pageconfig.java

Class pageconfig{

private static int pagesize=10;//shows 10 records per page

private static int pageno=1;//actually page 1

}

3. Method implementation Class Unionstudentserviceimpl.java

@service

@Transactional (Rollbackfor=runtimeexception.class)

Class Unionstudentserviceimpl Implments unionstudentservice{

@Autowired

Private Userdao Userdao;

/**

* Pagination Method

*pageconfig Pagination Class

*sex search Criteria

*/

@Override

Public list<unionstudent> getinfoforpage (pageconfig pageconfig,string sex) {
StringBuffer sbuffer=new StringBuffer ();
String sql1= "Select T1.studentname as name,t2.studentsex as sex from Student1 T1";
String sql1= "Select T2.studentname as name,t2.studentsex as sex from Student2 T2";
String sql1= "Select T3.studentname as name,t3.studentsex as sex from Student3 T3";
String conn= "union ALL";
if (sex!=null) {
Sbuffer.append ("SELECT * FROM ("). Append (SQL1). APPEND (conn). Append (SQL2) append (conn). Append (Sql3). Append (") where sex= ' "+sex+" ");
}else{
Sbuffer.append (SQL1). APPEND (conn). Append (SQL2). APPEND (conn). Append (SQL3);
}
Query query = Userdao
. Createsqlquery (Sbuffer.tostring ())
. Addscalar ("name", Stringtype.instance)
. Addscalar ("Sex", stringtype.instance)
. Setresulttransformer (
Transformers.aliastobean (Unionstudent.class));
Query.setfirstresult ((pageconfig.pageno-1) * pageconfig.pagesize);
Query.setmaxresults (pageconfig.pagesize);
list<unionstudent> list = Query.list ();
return list;


}

/**

* Get the number of data bars that meet the requirements

*sex search Criteria

*/

@Override
public int getsize (String sex) {

StringBuffer sbuffer=new StringBuffer ();
String sql1= "Select T1.studentname as name,t2.studentsex as sex from Student1 T1";
String sql1= "Select T2.studentname as name,t2.studentsex as sex from Student2 T2";
String sql1= "Select T3.studentname as name,t3.studentsex as sex from Student3 T3";
String conn= "union ALL";
if (sex!=null) {
Sbuffer.append ("SELECT * FROM ("). Append (SQL1). APPEND (conn). Append (SQL2) append (conn). Append (Sql3). Append (") where sex= ' "+sex+" ");
}else{
Sbuffer.append (SQL1). APPEND (conn). Append (SQL2). APPEND (conn). Append (SQL3);
}
Query query = Userdao
. Createsqlquery (Sbuffer.tostring ())
. Addscalar ("name", Stringtype.instance)
. Addscalar ("Sex", stringtype.instance)
. Setresulttransformer (
Transformers.aliastobean (Unionstudent.class));
list<unionstudent> list = Query.list ();
if (list = null && list.size ()! = 0) {
return List.size ();
} else {
return 0;
}
}

}

4.Spring Controller Class Studentcontroller.java

@Controller

public class studentcontroller{

@Autowired

Private Unionstudentservice Unionstudentservice;


@RequsetMapping (value= "/forpage")

@ResponseBody

Public map<string,object> forpage (httpservletrequest Request,model Model) {

String sex = request.getparameter ("Sex");
String curpage = Request.getparameter ("CurrentPage");
Pageconfig pageconfig = new Pageconfig ();
if (Stringutils.hastext (curpage)) {
Pageconfig.pageno = Integer.parseint (curpage);
}
list<unionstudnet> unionstudnetlist = unionstudentservice.getforpage (pageconfig, sex);
int allpage = unionstudentservice.getsize (Sex);
int pageSize = (int) Math.ceil ((double) allpage/pageconfig.pagesize);

map<string, object> map = new hashmap<string, object> ();
Map.put ("CurrentPage", Pageconfig.pageno);
Map.put ("Allpage", allpage);
Map.put ("PageSize", pageSize);
Map.put ("Unionstudnetlist", unionstudnetlist);
return map;

}

}

5. Interface Display index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "Iso-8859-1"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<script src= "<%=basepath%>statics/js/jquery-1.7.2.min.js" ></script>
<script type= "Text/javascript" >
Select Page
$ (document). Ready (function () {
$ ("#pageNav a"). Click (function () {
var idx = $ ("#pageNav a"). Index ($ (this));//returns the current element indexed in the navigation's hyperlink collection
var curpageno=parseint ($ ("#currentPage"). html ());//Current page, because to calculate, note type conversion
var pagesize=$ ("#pageSize"). html ();//Total pages
var pageno=1;//the page to be requested
Switch (IDX) {
Case 0:
Pageno=1;
Break
Case 1:
Pageno=curpageno-1;
Break
Case 2:
pageno=curpageno+1;
Break
Case 3:
Pageno=pagesize;
Break
}
if (pageno>pagesize) {
Return
}else if (pageno<=0) {
Return
}
$.ajax ({
Type: ' POSt ',
URL: "<%=basepath%>forpage",
Data: "sex=" +sex+ "&currentpage=" +pageno,
Success:function (JSON) {
$ ("#currentPage"). HTML (json.currentpage);
var size=json.pagesize;
$ ("#pageSize"). html (size);

if (size==0) {
$ ("#selectPageUlNull"). Show ();
$ ("#selectPageUlInfo"). Hide ();
}else{
$ ("#selectPageUlNull"). Hide ();
$ ("#selectPageUlInfo"). Show ();
}
$ ("#productInfoArea"). Remove ();//delete elements that are not the first row
$ ("#productInfoAreaCenter"). HTML ("<ul id= ' Productinfoarea ' ></ul>");
$.each (Json.unionstudnetlist,function (idx,unionstudnet) {
$ ("#productInfoArea"). Append ("<li>+" +unionstudnet.name+ "+</li>" + "<li>+" +unionstudnet.sex+ "+ </li> ");
});
},
DataType: ' JSON ',
Async:false
});

});
});
</script>
<body>
<div class= "search-list" id= "Productinfoareacenter" >
<ul id= "Productinfoarea" ></ul>
</div>

<ul class= "Paging tc pdv30" id= "Selectpageulnull" style= "Display:none;" >
<span> query information does not exist. </span>
</ul>
<ul class= "Paging tc pdv30" id= "Selectpageulinfo" >
<span> Total </span>
<span id= "PageSize" >${pageSize}</span>
<span> pages </span> &nbsp;&nbsp;
<span> Current Section </span>
<span id= "CurrentPage" >${currentPage}</span>
<span> page </span>
&nbsp;&nbsp;
<span id= "Pagenav" >
<a href= "javascript:void (0)" > Home </a>
<a href= "javascript:void (0)" > previous </a>
<a href= "javascript:void (0)" > Next </a>
<a href= "javascript:void (0)" > Last </a>
</span>

<span> Skip to section </span>
<span>
<select name= "pages" id= "Pageselect" onchange= "Pagechange ()" boxwidth= "selwidth=" >
<c:foreach var= "Index" begin= "1" end= "${pagesize}" step= "1" >
<c:if test= "${currentpage==index}" >
<option value= "${index}" selected= "selected" >${index}</option>
</c:if>
<c:if test= "${currentpage!=index}" >
<option value= "${index}" >${index}</option>
</c:if>
</c:forEach>
</select>
</span>
<span> page </span>
</ul>
</body>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.