자바 진급 의 Aiax 무 리 셋 페이지 지식 포인트 총화
23700 단어 자바 진급
Web , 。 Ajax 。 , 1 , “ ”、“ ”、“ ” “ ” , , 。
그림 1 페이지 리 셋 없 음 실현
Ajax 。 , Ajax , , XML 。 Ajax XML , , , 。
(1) request.js , Ajax 。
(2) index.jsp, Ajax request.js , <script> Ajax XMLHttpRequest 。 JavaScript getData(), request.js Ajax 。 :</span></pre>
<p></p>
<pre><code class="language-java"><script language="javascript" src="js/request.js">
var request = false; // XMLHttpRequest
//Ajax
function getData(page){
var url="GoodsServlet"; //
var param ="page="+page+"&nocache="+new Date().getTime();//
request=httpRequest("post",url,true,callbackFunc,param);// request.js
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">(3) Servlet GoodsServlet, 。 doPost() Ajax page , MIME XML , XML , XML 。doPost() :</span></pre>
<pre><code class="language-java">public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType( "text/xml;charset=UTF-8" );// test/xml
// response.addHeader(),
response.addHeader( "Cache-Control", "no-store,no-cache,must-revalidate" );
response.addHeader( "Cache-Control", "post-check=0,pre-check=0" );
response.addHeader( "Expires", "0" );
response.addHeader( "Pragma", "no-cache" );
PrintWriter out = response.getWriter();
PageElement pageArgs = getPageArgs(); //
List goodses = null; //
//
int page=1;
if(null!=request.getParameter("page")){
page = Integer.parseInt(request.getParameter("page"));
}
pageArgs.setPageCount(page);
out.println( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" );
out.println( "<goodses>" );
out.println( "<pageElement>" );
out.println( "<pageNum>" +pageArgs.getPageNum()+"</pageNum>");
out.println( "<maxPage>" +pageArgs.getMaxPage()+"</maxPage>");
out.println( "<nextPage>" +pageArgs.getNextPage()+"</nextPage>");
out.println( "<prePage>" +pageArgs.getPrePage()+"</prePage>");
out.println( "</pageElement>" );
try{
goodses = getGoods(page);
}catch(Exception e){
e.printStackTrace();
}
for(int i=0;i<goodses.size();i++){
CommodityForm commodity = (CommodityForm) goodses.get(i);
out.println( "<goods>" );
out.println( "<id>"+commodity.getId()+"</id>" );
out.println( "<goodsName>"+commodity.getGoodsName()+"</goodsName>" );
out.println( "<introduce>"+commodity.getIntroduce()+"</introduce>" );
out.println( "<Price>"+commodity.getPrice()+"</Price>" );
out.println( "</goods>" );
}
out.println( "</goodses>" );
out.close();
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">(4) PageElement , 。 :</span></pre>
<pre><code class="language-java">public class PageElement {
private int pageNum; //
private int pageSize; //
private long maxPage; //
private int prePage; //
private int nextPage; //
public void setPageCount(int pageNum) {
this.pageNum = pageNum;
//
prePage=pageNum-1<=1?1:pageNum-1;
//
nextPage=(int)(pageNum + 1 >= maxPage ? maxPage : pageNum + 1);
}
... // getXXX() setXXX()
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">(5) GoodsServlet , getPageArgs() , :</span></pre>
<pre><code class="language-java">public PageElement getPageArgs() {
PageElement pag = null; //
Statement stmt = null;
ResultSet rs = null;
ConnDB conn = new ConnDB(); //
try {
// SQL
String sql = "SELECT count(*) FROM tb_goods";
rs = conn.executeQuery(sql); // SQL
if (rs.next()) {
int count = rs.getInt(1); //
pag = new PageElement(); //
pag.setPageSize(pagesize); //
pag.setMaxPage((count + pagesize - 1) / pagesize);//
pag.setPageCount(count); //
}
} catch (SQLException ex) {
ex.getMessage();
} finally {
conn.close();
}
return pag;
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">(6) GoodsServlet , getGoods() , :</span></pre>
<pre><code class="language-java">public List getGoods(final int page) throws Exception {
List list = new ArrayList(); //
ConnDB conn = new ConnDB(); //
CommodityForm f =null;
int firstResult = (page-1) * pagesize;
try {
// SQL
String sql= "select * from tb_goods order by id limit "+firstResult+","+pagesize;
ResultSet rs = conn.executeQuery(sql); //
while (rs.next()) { //
f = new CommodityForm(); //
f.setId(rs.getInt("id"));
f.setGoodsName(rs.getString("name"));
f.setIntroduce(rs.getString("introduce"));
f.setPrice(rs.getFloat("price"));
list.add(f); // List
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return list;
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">7) GoodsServlet , index.jsp , Ajax callbackFunc(), Ajax GoodsServlet XML , <span> 。callbackFunc() :</span></pre>
<pre><code class="language-java">function callbackFunc(){
if(request.readyState == 4){
if(request.status == 200){
var doc = request.responseXML;
var pageNum = doc.getElementsByTagName("pageNum")[0].firstChild.data;
var maxPage= doc.getElementsByTagName("maxPage")[0].firstChild.data;
var prePage = doc.getElementsByTagName("prePage")[0].firstChild.data;
var nextPage = doc.getElementsByTagName("nextPage")[0].firstChild.data;
var goodses = doc.getElementsByTagName("goods");
var innerHTML = "";
if((goodses!=null)&&(goodses.length!=0)) {
innerHTML+="<table width='96%' border='1' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF' bordercolordark='#FFFFFF' bordercolorlight='CFCFCF'>";
innerHTML+="<tr align='center' height='30'><td> </td>";
innerHTML+="<td> </td>";
innerHTML+="<td> </td></tr>";
for(var i=0;i<goodses.length;i++) {
var goods = goodses[i];
var goodsName = goods.childNodes[1].firstChild.data;
var introduce = goods.childNodes[2].firstChild.data;
var price = goods.childNodes[3].firstChild.data;
innerHTML += "<tr height='30'>";
innerHTML += "<td align='left'> "+goodsName+"</td>";
innerHTML += "<td align='left'> "+introduce+"</td>";
innerHTML += "<td align='left'> "+price+"</td>";
innerHTML += "</tr>";
}
innerHTML+="<tr height='30'><td align='center' colspan='6'>";
innerHTML +="["+ pageNum+"/"+maxPage+"] ";
innerHTML+="<a href=\"javascript:void(0)\" onClick=\"getData(1)\">[ ]</a> ";
innerHTML+= "<a href=\"javascript:void(0)\" onClick=\"getData("+prePage+")\">[ ]</a> ";
innerHTML+= "<a href=\"javascript:void(0)\" onClick=\"getData("+nextPage+")\">[ ]</a> ";
innerHTML +="<a href=\"javascript:void(0)\" onClick=\"getData("+maxPage+")\">[ ]</a>";
innerHTML+="</td></tr>
";
innerHTML+="</table>
";
}else {
innerHTML += " ";
}
document.getElementById("goodsList").innerHTML = innerHTML;
request = false;// XMLHttpRequest
}
}
}
</code></pre>
<pre><span style="font-family:'Microsoft YaHei';font-size:14px;">(8) index.jsp <span> , id “goodsList”。 :
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"> <span id=<em>"goodsList</em>"></span>
</span><span style="font-family:'Microsoft YaHei';font-size:14px;">(9) , 。 :
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"> window.οnlοad=<strong>function</strong>(){
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"> getData(1);
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"> }
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"><span style="color:#FF0000;"> </span><strong>
</strong></span><span style="font-family:'Microsoft YaHei';font-size:14px;"> XML , JavaScript DOM 。 DOM ,HTML Node , HTML Node , , 。DOM Node parentNode 、firstChild 、lastChild 、lastChild 、previousSibling nextSibling 。Node 1 。
</span><span style="font-family:'Microsoft YaHei';font-size:14px;"> 1 Node </span></pre>
<table border="1" width="547">
<tbody>
<tr>
<td style="background:#D9D9D9;"><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
<td style="background:#D9D9D9;"><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
<td style="background:#D9D9D9;"><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">parentNode</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">Node</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> , null</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">childNodes</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">NodeList</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> NodeList</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">firstChild</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">Node</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> , null</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">lastChild</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">Node</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> , null</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">previousSibling</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">Node</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> , null</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">nextChild</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">Node</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> , null</span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">nodeName</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">String</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">nodeValue</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">String</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
</tr>
<tr>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">nodeType</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;">short</span></p></td>
<td><p><span style="font-family:'Microsoft YaHei';font-size:14px;"> </span></p></td>
</tr>
</tbody>
</table>
<span style="font-family:'Microsoft YaHei';font-size:14px;"> </span>
<span style="font-family:'Microsoft YaHei';font-size:14px;"> </span>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
<!--PC WAP -->
<div id="SOHUCS" sid="1296064097535729664"></div>
<script type="text/javascript" src="/views/front/js/chanyan.js">
이 가능 하 다, ~ 할 수 있다,...lijingyao8206
바이트 코드 기술 jvm AOP 동적 에이전트 ASM
백합 은 차 가 아니다.
java 내부 류 익명 내부 클래스
crabdave
web.xml
밀밭 설계 자
java android 범 형
IT 독행 자
JavaScript UI css
_wy_
jdk windows cygwin
무량 하 다
maven linux 설치 하 다.
aichenglong
tomcat http 에서 https 로 이동 syslong - ng 설정 syslog 설정
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 기타
첫 페이지 -
우리 -
역 내 검색 -
Sitemap -
권리 침해 신고
모든 IT 지식 창고 저작권© 2000 - 2050 IT 지식 창고 IT610. com, 판권 소유.
경 ICP 비 09083238 호