---
title: "对最近项目问题的一个思考。（CMS系统和AJAX）"
author: "mhbjava"
date: "2006-08-04T14:50:00Z"
tags: []
slug: "cms-ajax"
draft: false
lastmod: "2006-08-04T14:50:00Z"
id: 26595
url: "https://mhbjava.iteye.com/blog/26595"
---

对最近项目问题的一个思考。

1个重要的js文件:tools.js,对几乎用到的最基本的js进行了封装

项目用到了cms。  
在cms里用ajax有2种方式。

第一：(class=on的先是默认的打开)  
导入js文件，必须有先后顺序  
&lt;script src="[http://static.duniu.com/www/web/js/tools.js](http://static.duniu.com/www/web/js/tools.js)" type="text/javascript"&gt;&lt;/script&gt;  
&lt;script src="[http://static.duniu.com/www/web/js/TabContentAjax.js](http://static.duniu.com/www/web/js/TabContentAjax.js)" type="text/javascript"&gt;&lt;/script&gt;

&lt;ul id="billboardTab"&gt;  
 &lt;li class="on"&gt;1。&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=1](http://www.duniu.com/cmsGuildOrder?type=1)"&gt;积分榜&lt;/a&gt;&lt;/li&gt;  
 &lt;li&gt;2。&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=2](http://www.duniu.com/cmsGuildOrder?type=2)"&gt;人数榜&lt;/a&gt;&lt;/li&gt;  
 &lt;li&gt;&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=3](http://www.duniu.com/cmsGuildOrder?type=3)"&gt;人气榜&lt;/a&gt;&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="billboardCont"&gt;  
&lt;/div&gt;

注意:1.   2.    这些东西都不能要。 class="on" 是默认打开的

&lt;script language="javascript"&gt;  
 new TabContentAjax("billboardTab", "billboardCont");//必须放在后面  
&lt;/script&gt;

会把通过controller返回来的vm放到&lt;div id="billboardCont"&gt;&lt;/div&gt;这个&lt;div&gt;里。  
Controller里map里放一个值:map.put("URL","digitalproduct?type="+type +"&pageId=");

返回的vm里页码为:  
#foreach($item in $list)  
    &lt;div&gt;  
    &lt;a href="newcp\_more.html"&gt;&lt;img src="imgs/cp/newcp01.gif"/&gt;&lt;/a&gt;&lt;h1&gt;&lt;a href="newcp\_more.html"&gt;$!item.Title&lt;/a&gt;&lt;/h1&gt;&lt;h2&gt;$!item.Time&lt;/h2&gt;  
    &lt;p&gt;$!item.Content&lt;/p&gt;  
    &lt;/div&gt;  
#end  
&lt;div id="pageContent"&gt;  
  #set($url=$!URL)  
  #set($changepage="pageAjax('billboardCont',this.href);return false;")//有事件处理  
  #parse("cms/page.vm")  
&lt;/div&gt;

 

 

第二：(通过资源类先生成一个)  
导入js文件，必须有先后顺序  
&lt;script src="[http://static.duniu.com/www/web/js/tools.js](http://static.duniu.com/www/web/js/tools.js)" type="text/javascript"&gt;&lt;/script&gt;  
&lt;script src="[http://static.duniu.com/www/web/js/ArticleContentAjax.js](http://static.duniu.com/www/web/js/ArticleContentAjax.js)" type="text/javascript"&gt;&lt;/script&gt;

&lt;ul id="billboardTab"&gt;  
 &lt;li&gt;1。&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=1](http://www.duniu.com/cmsGuildOrder?type=1)"&gt;积分榜&lt;/a&gt;&lt;/li&gt;  
 &lt;li&gt;2。&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=2](http://www.duniu.com/cmsGuildOrder?type=2)"&gt;人数榜&lt;/a&gt;&lt;/li&gt;  
 &lt;li&gt;&lt;a href="[http://www.duniu.com/cmsGuildOrder?type=3](http://www.duniu.com/cmsGuildOrder?type=3)"&gt;人气榜&lt;/a&gt;&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="billboardCont"&gt;  
&lt;/div&gt;

注意:1.   2.    这些东西都不能要。

&lt;script language="javascript"&gt;  
   
new ArticleContentAjax("billboardTab", "billboardCont");  
new ArticleContentAjax("pageContent","billboardCont");  //分页的。  
&lt;/script&gt;

页码是通过资源类生成的。  
//String URL="/c\_"+ urlNum +"\_p\_";//c\_191\_p\_5.htm ,点分页时会自动把页码加在后面，资源类里本来是通过这个URL来实现整个页面的刷新。  
String URL = "/digitalproduct?pageId=";//现在通过这个来生成出页码，页码所在的&lt;div &gt;的class 为pageContent

点页码时转向Controller.但是因为后面有个.htm请求。所以要把.htm给解掉。  
所以Controller里有个截取的方法  
if(strPageId!=null && strPageId.trim().length() &gt; 0){  
int pos=strPageId.indexOf(".htm");  
String idStr=null;  
if(pos!=-1){  
 idStr=strPageId.substring(0,pos);  
 try{  
  pageId=Integer.parseInt(idStr);  
 }catch(Exception e){  
  e.printStackTrace();  
 }      
   
}else{  
 try{  
  pageId=Integer.parseInt(strPageId);  
 }catch(Exception e){  
  e.printStackTrace();  
 }    
}

}

会把通过controller返回来的vm放到&lt;div id="billboardCont"&gt;&lt;/div&gt;这个&lt;div&gt;里。  
Controller里map里放一个值:map.put("URL","digitalproduct?type="+type +"&pageId=");

返回的vm里页码为:  
#foreach($item in $list)  
    &lt;div&gt;  
    &lt;a href="newcp\_more.html"&gt;&lt;img src="imgs/cp/newcp01.gif"/&gt;&lt;/a&gt;&lt;h1&gt;&lt;a href="newcp\_more.html"&gt;$!item.Title&lt;/a&gt;&lt;/h1&gt;&lt;h2&gt;$!item.Time&lt;/h2&gt;  
    &lt;p&gt;$!item.Content&lt;/p&gt;  
    &lt;/div&gt;  
#end  
&lt;div id="pageContent"&gt;  
  #set($url=$!URL)  
  #set($changepage="pageAjax('billboardCont',this.href);return false;")//有事件处理  
  #parse("cms/page.vm")  
&lt;/div&gt;