Jquery+Ajax+JSon 을 사용 하여 페이지 표시 JAVA+JQuery 를 붙 여 비동기 페이지 를 만 드 는 방법
1.배경 action 에서 json 데 이 터 를 생 성 합 니 다.
List blackList = blackService.getBlackInfoList(mobileNum, gatewayid, startDate, endDate);
int totalRows = blackList.size();
StringBuffer sb = new StringBuffer();
sb.append("{\"totalCount\":\""+totalRows+"\",");
sb.append("\"jsonRoot\":[");
for (int i=0;i<blackList.size();i++) {
LBlack blackInfo = (LBlack)blackList.get(i);
sb.append("{\"id\":\""+ blackInfo.getId());
sb.append("\",");
sb.append("\"mobile\":\""+ blackInfo.getMobile());
sb.append("\",");
sb.append("\"province\":\""+ blackInfo.getProvince());
sb.append("\",");
sb.append("\"gateway\":\""+ blackInfo.getGateway());
sb.append("\",");
sb.append("\"insertTime\":\""+ blackInfo.getInsertTime());
sb.append("\",");
sb.append("\"remark\":\""+ blackInfo.getRemark());
sb.append("\"");
sb.append("},");
}
sb.deleteCharAt(sb.lastIndexOf(",")); //
sb.append("]}");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/plain");
response.getWriter().print(sb);
2.struts.xml 관련 설정
<action name="blackList" class="blackAction" method="blackList">
<!--plaintext -->
<result type="plainText">
<param name="charSet">UTF-8</param>
<param name="location">/WEB-INF/jsp/manage/black.jsp</param>
</result>
</action>
3.js json 데이터 페이지 표시 가 져 오기
function getJSONData(pn) {
// alert(pn);
$.getJSON("blackList.ce", function(data) {
var totalCount = data.totalCount; //
var pageSize = 10; //
var pageTotal = Math.ceil(totalCount / pageSize); //
var startPage = pageSize * (pn - 1);
var endPage = startPage + pageSize - 1;
var $ul = $("#json-list");
$ul.empty();
for (var i = 0; i < pageSize; i++) {
$ul.append('<li class="li-tag"></li>');
}
var dataRoot = data.jsonRoot;
if (pageTotal == 1) { //
for (var j = 0; j < totalCount; j++) {
$(".li-tag").eq(j).append("<span class='col1'><input type='checkbox' value='"+parseInt(j + 1)+"'/></span>")
.append("<span class='col2'>" + parseInt(j + 1)
+ "</span>").append("<span class='col3'>" + dataRoot[j].mobile
+ "</span>").append("<span class='col4'>" + dataRoot[j].province
+ "</span>").append("<span class='col5'>" + dataRoot[j].gateway
+ "</span>").append("<span class='col6'>" + dataRoot[j].insertTime
+ "</span>").append("<span class='col7'>" + dataRoot[j].remark
+ "</span>")
}
} else {
for (var j = startPage, k = 0; j < endPage, k < pageSize; j++, k++) {
if( j == totalCount){
break; // ,
}
$(".li-tag").eq(k).append("<span class='col1'><input type='checkbox' value='"+parseInt(j + 1)+"'/></span>")
.append("<span class='col2'>" + parseInt(j + 1)
+ "</span>").append("<span class='col3'>" + dataRoot[j].mobile
+ "</span>").append("<span class='col4'>" + dataRoot[j].province
+ "</span>").append("<span class='col5'>" + dataRoot[j].gateway
+ "</span>").append("<span class='col6'>" + dataRoot[j].insertTime
+ "</span>").append("<span class='col7'>" + dataRoot[j].remark
+ "</span>")
}
}
$(".page-count").text(pageTotal);
})
}
function getPage() {
$.getJSON("blackList.ce", function(data) {
pn = 1;
var totalCount = data.totalCount; //
var pageSize = 10; //
var pageTotal = Math.ceil(totalCount / pageSize); //
$("#next").click(function() {
if (pn == pageTotal) {
alert(" ");
pn = pageTotal;
} else {
pn++;
gotoPage(pn);
}
});
$("#prev").click(function() {
if (pn == 1) {
alert(" ");
pn = 1;
} else {
pn--;
gotoPage(pn);
}
})
$("#firstPage").click(function() {
pn = 1;
gotoPage(pn);
});
$("#lastPage").click(function() {
pn = pageTotal;
gotoPage(pn);
});
$("#page-jump").click(function(){
if($(".page-num").val() <= pageTotal && $(".page-num").val() != ''){
pn = $(".page-num").val();
gotoPage(pn);
}else{
alert(" !");
$(".page-num").val('').focus();
}
})
$("#firstPage").trigger("click");
})
}
function gotoPage(pn) {
// alert(pn);
$(".current-page").text(pn);
getJSONData(pn)
}
$(function() {
getPage();
})
ps:JAVA+JQuery 비동기 페이지 구현최근 한 항목 이 비동기 페이지 를 실현 하 라 고 요구 하여 간단하게 썼 습 니 다.좋 지 않 은 것 은 지적 해 주세요~
/**
*
*/
public class PageBean {
publicint rowCount = 0; //
publicint currentPage = 1;//
publicint sizePerPage = 20;//
publicint pageCount = 0;//
publicString pageURL;// URL
publicString pageDisplay;// JSP
publicString pageStyle = "numberStyle";//
publicint pagePreOffset = 10;//
publicint pageNextOffset = 9;//
publicString pageCss;//
publicString getPageCss() {
returnpageCss;
}
publicvoid setPageCss(String pageCss) {
this.pageCss = pageCss;
}
publicString getPageStyle() {
returnpageStyle;
}
publicvoid setPageStyle(String pageStyle) {
this.pageStyle = pageStyle;
}
publicint getPagePreOffset() {
returnpagePreOffset;
}
publicvoid setPagePreOffset(intpagePreOffset) {
this.pagePreOffset = pagePreOffset;
}
publicint getPageNextOffset() {
returnpageNextOffset;
}
publicvoid setPageNextOffset(intpageNextOffset) {
this.pageNextOffset = pageNextOffset;
}
publicString getPageDisplay() {
String nextClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.currentPage + 1)+"");return false;' ";
String preClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.currentPage - 1)+"");return false;' ";
String firstClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=1");return false;' ";
String lastClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.getPageCount())+"");return false;' ";
String onChange=" onchange='ajaxpage(""+this.pageURL+ "?currentPage=" + (1)+"");return false;' ";
StringBuffer pageString =new StringBuffer();
pageString.append("<div class='"+ this.pageStyle +"'><span >");
//
if("numberStyle".equalsIgnoreCase(this.pageStyle)) {
// ,
if(this.getPageCount() ==1) {
// pageString.append("<strong> 1</strong> ");
}else {
if(this.currentPage >1) {// 1,<< <
pageString.append("<a class='pagination-first' "+firstClick+" title=' ' href='" + this.pageURL
+"?currentPage=1'><<</a> ");
pageString.append("<a class='pagination-prev' "+preClick+"title=' ' href='" + this.pageURL
+"?currentPage=" + (this.currentPage -1)
+"'><</a> ");
}else {
pageString
.append("<a class='pagination-first'><<</a> ");
pageString
.append("<a class='pagination-prev'><</a> ");
}
//
intpreOffset = this.currentPage -1 > this.pagePreOffset ?this.pagePreOffset
:this.currentPage -1;
//
intnextOffset = this.getPageCount() -this.currentPage >this.pageNextOffset ?this.pageNextOffset
:this.getPageCount() -this.currentPage;
// ,
for(int i = (this.currentPage - preOffset); i <= (this.currentPage + nextOffset); i++) {
String numClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (i)+"");return false;' ";
if(this.currentPage == i) {//
pageString
.append("<strong style='color:black;border:0'>"
+ i +"</strong> ");
}else {
pageString.append("<a "+numClick+"href='"+ this.pageURL
+"?currentPage=" + i + "'>" + i +"</a> ");
}
}
// ,> >>
if(this.currentPage <this.getPageCount()) {
pageString.append("<a class='pagination-next' "+nextClick+" title=' ' href='" + this.pageURL
+"?currentPage=" + (this.currentPage +1)
+"'>></a> ");
pageString.append("<a class='pagination-last' "+lastClick+"title=' ' href='" + this.pageURL
+"?currentPage=" + (this.getPageCount()) +"'>>></a> ");
}else {
pageString
.append("<a class='pagination-next' >></a> ");
pageString
.append("<a class='pagination-last'>>></a> ");
}
pageString.append("<select id='pageSelect' "+onChange+">"+this.getOptions()+"</select>");
}
}else if("normalStyle".equalsIgnoreCase(this.pageStyle)) {
if(this.getPageCount() ==1) {
pageString.append("<strong> 1 </strong> ");
}else {
if(this.currentPage >1) {
pageString.append("<a class='pagination-first' "+firstClick+" title=' ' href='" + this.pageURL
+"?currentPage=1'><<</a> ");
pageString.append("<a class='pagination-prev' "+preClick+"title=' ' href='" + this.pageURL
+"?currentPage=" + (this.currentPage -1)
+"'><</a> ");
}else {
pageString
.append("<a class='pagination-first'><<</a> ");
pageString
.append("<a class='pagination-prev'><</a> ");
}
pageString.append("<span class='pageinfo'> "+this.currentPage+" /"+this.pageCount+" </span>");
if(this.currentPage <this.getPageCount()) {
pageString.append("<a class='pagination-next' "+nextClick+" title=' ' href='" + this.pageURL
+"?currentPage=" + (this.currentPage +1)
+"'>></a> ");
pageString.append("<a class='pagination-last' "+lastClick+"title=' ' href='" + this.pageURL
+"?currentPage=" + (this.getPageCount()) +"'>>></a> ");
}else {
pageString
.append("<a class='pagination-next' >></a> ");
pageString
.append("<a class='pagination-last'>>></a> ");
}
pageString.append("<select id='pageSelect' "+onChange+">"+this.getOptions()+"</select>");
}
}
pageString.append("</span></div>");
this.pageDisplay = pageString.toString();
returnpageDisplay;
}
publicvoid setPageDisplay(String pageDisplay) {
this.pageDisplay = pageDisplay;
}
publicString getPageURL() {
returnpageURL;
}
publicvoid setPageURL(String pageURL) {
this.pageURL = pageURL;
}
publicint getPageCount() {
this.pageCount =this.rowCount %this.sizePerPage ==0 ? (this.rowCount /this.sizePerPage)
: (this.rowCount /this.sizePerPage) +1;
returnthis.pageCount;
}
publicvoid setPageCount(intpageCount) {
this.pageCount = pageCount;
}
publicint getRowCount() {
returnrowCount;
}
publicvoid setRowCount(introwCount) {
this.rowCount = rowCount;
}
publicint getCurrentPage() {
returncurrentPage;
}
publicvoid setCurrentPage(intcurrentPage) {
this.currentPage = currentPage;
}
publicint getSizePerPage() {
returnsizePerPage;
}
publicvoid setSizePerPage(intsizePerPage) {
this.sizePerPage = sizePerPage;
}
privateString getOptions(){
StringBuffer sb =new StringBuffer();
switch(this.sizePerPage) {
case10:
sb.append("<option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=50>50</option><option value=100>100</option>");
break;
case20:
sb.append("<option value=20>20</option><option value=10>10</option><option value=30>30</option><option value=50>50</option><option value=100>100</option>");
break;
case30:
sb.append("<option value=30>30</option><option value=10>10</option><option value=20>20</option><option value=50>50</option><option value=100>100</option>");
break;
case50:
sb.append("<option value=50>50</option><option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=100>100</option>");
break;
case100:
sb.append("<option value=100>100</option><option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=50>50</option>");
break;
}
returnsb.toString();
}
}
//
PageBean page = new PageBean();
setPageInfo(list,request);
public void setPageInfo(List list,HttpServletRequest request){
page.setCurrentPage(this.getCurrentPage());
if(request.getParameter("perSize")==null){
page.setSizePerPage(20);//default 20
}
else{
page.setSizePerPage(Integer.valueOf(request.getParameter("perSize")));
}
page.setRowCount(list.size());
//page.setPageStyle("normalStyle");
//page.setPagePreOffset(5);//default 10
//page.setPageNextOffset(4);//default 9
page.setPageURL(request.getRequestURL().toString());
}
[css] view plaincopyprint?
/**
** CSS
*/
.numberStyle,.normalStyle {
text-align:left;
}
.numberStyle a,.normalStyle a {
display: inline-block;
color: #5489F1;
text-decoration: none;
font-size: 14px;
font-weight:bold;
font-family: Geneva, Arial, Helvetica, sans-serif;
border: 1px solid #999;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
background-position:center;
}
.numberStyle strong,.normalStyle strong {
display: inline-block;
color: #5489F1;
text-decoration: none;
font-size: 14px;
font-weight:bold;
font-family: Geneva, Arial, Helvetica, sans-serif;
border: 1px solid #999;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
background-position:center;
}
.numberStyle a:hover,.normalStyle a:hover{
background-color: #d0d0d0;
}
.normalStyle .pageinfo{
font-size: 14px;
font-family: Geneva, Arial, Helvetica, sans-serif;
color: #5489F1;
}
[javascript] view plaincopyprint?
/**
** JS import jquery.js before call function
*/
function ajaxpage(action){
action=action+"&perSize="+$("#pageSelect").val();
$.ajax( {
type : "POST",
url : action,
success : function(msg) {
// , msg
// ,
// :$("#displayTable>thead").nextAll().remove();$("#displayTable").append(msg);
}
});
}
다음은 본 고 에서 소개 한 Jquery+Ajax+JSon 을 사용 하여 페이지 디 스 플레이 에 JAVA+JQuery 를 붙 여 비동기 페이지 를 실현 하 는 방법 입 니 다.도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
jQuery 전후 예이 기사에서는 jquery after() 및 before() 메소드의 예를 볼 것입니다. before() 메서드는 선택한 요소 앞에 지정된 콘텐츠를 삽입합니다. after() 메서드는 선택한 요소 뒤에 지정된 콘텐츠...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.