?? artshow.java
字號(hào):
package com.yhcms.article.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yhcms.utils.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yhcms.article.bean.Article;
import com.yhcms.article.dao.ArticleDaoIm;
import com.yhcms.article.itface.ArticleDao;
import com.yhcms.db.DBConnException;
import com.yhcms.utils.ReqUtils;
/**
* <p>Title:系統(tǒng)顯示文章Action</p>
* <li>顯示文章</li>
* <li>從Request取得文章Id,然后取得文章對(duì)象顯示</li>
* <br><b>CopyRight: yyhweb[由由華網(wǎng)]</b>
* @author stephen
* @version YH-2.0
*/
public class ArtShow extends Action {
public ActionForward execute(ActionMapping actionmapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
Logger yhlog = Logger.getLogger(ArtShow.class.getName());
String content = "";
int bid = 0;
int artId = ReqUtils.getInt(request,"artId");
int p = ReqUtils.getInt(request,"p");
String linkInfo = "";
int cLength = 0; // 文章內(nèi)容的長(zhǎng)度
int pgCount = 0; // 文章頁(yè)數(shù)
int pSize = 3000;
int tmpl = 0;
int tmps = 0;
List pnlist = new ArrayList(); // 該文章相關(guān)文章列表
ArticleDao artdao = ArticleDaoIm.getInstance();
Article curArt = null; // 此時(shí)的Article對(duì)象 只需要一個(gè)Dto 就可以了,故此處用到ArtShowDto
try {
curArt = (Article)artdao.getArticleById(artId);
if(curArt!=null){
bid = curArt.getBid();
pnlist = artdao.getPreNextArt(artId,bid);
if(p==0){
artdao.addOneReadTime(artId);
}
content = curArt.getContent();
curArt.setContent("");
cLength = content.length();
tmpl = cLength/pSize;
if(cLength > pSize){
if(p<=0){
p = 1;
}
tmps = p*pSize;
pgCount = (cLength%pSize)!=0?(tmpl+1):tmpl;
linkInfo = subPageLink(pgCount,artId,p,"ArtShow.html?artId="+artId);
content = content.substring((p-1)*pSize,tmps>cLength?cLength:tmps);
}
}else{
curArt = new Article();
// 取得文章出錯(cuò)或者artId<=0的話,記錄一個(gè)錯(cuò)誤.
yhlog.info("Get an article unsuccessfully.");
request.setAttribute("article",curArt);
return actionmapping.findForward("Show");
}
}catch (DBConnException e) {
yhlog.warn("When get an article to show,throw an Exception!The article id is:"+artId+".");
}
content = StringUtils.ubbEncode((content));
request.setAttribute("article",curArt);
request.setAttribute("cInfo",linkInfo);
request.setAttribute("content",content);
request.setAttribute("pnlist",pnlist);
return actionmapping.findForward("Show");
}
/**
* @param c 文章分頁(yè)數(shù)目
* @param artId 文章Id
* @param p 當(dāng)前頁(yè)碼
* @param link 鏈接字符串
* @return 鏈接字符串
*/
public String subPageLink(int c,int artId,int p,String link){
String str = "";
StringBuffer sb = new StringBuffer();
sb.append("文章共 "+c+" 頁(yè) [ ");
for(int i=0;i<c;i++){
if((i+1)==p){
sb.append("<b>");
sb.append((i+1));
sb.append("</b>");
sb.append(" ");
continue;
}
sb.append("<a href=");
sb.append(link+"&p="+(i+1)+">");
sb.append((i+1));
sb.append("</a>");
sb.append(" ");
}
sb.append(" ]");
str = sb.toString();
sb = null;
return str;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -