對天乙社區bbscs8實現的詳細分析二十四

好的,我們看jsp文件:post.jsp,對於這個文件,我們要注意的是有些值加入了判斷是不是 <s:if test="%{action=='editdo'}">或者是<s:if test="%{action=='addsave'}">可見用戶在修改自己的帖子時也用的是這個文件.當用戶編輯的時候,用戶簽名之後的選項都被disabled="true"了或者去掉了(如隱藏帖選項),當我們提交後,<s:form action="post">在js之後進行觸發:
<script type="text/javascript">
function postSubmit() {
document.getElementById("postButton").disabled = true;
document.post.submit();
}
</script>
另外,這個文件中的:
<s:actionerror theme="bbscs0"/> //錯誤提示信息
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
    <td class="bgColor3">
    <bbscs:post type="postat"/>
/**在bbscs.tld中<tag>
<name>post</name>
<tag-class>com.laoer.bbscs.web.taglib.PostTag</tag-class>
<body-content>empty</body-content>
<attribute>
   <name>type</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
   <name>boardValue</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
   <name>tagIdValue</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
-->postTag.java中:
protected String type = "";
protected String boardValue = "%{board}";
protected String tagIdValue = "%{tagId}";
實際上是if (type.equalsIgnoreCase("postat")) {
     this.write(writer, messageSource.getMessage("post.youatboards", new String[] { sb.toString() },
       request.getLocale()));
    }/您目前在 社區技術 區發表信息
*/
    </td>
</tr>
</table>
OK!我們看addsave方法,注意它充當了驗證的功能:
try {              //上傳文件的部分!
    uploadFile = new UploadFile();
    uploadFile.setFileName(this.getUploadFileName().toLowerCase());
    uploadFile.setInputStream(new FileInputStream(this.getUpload()));
   } catch (FileNotFoundException e) {
    logger.error(e);
    uploadFile = null;
   }
}
..
Forum f = new ForumMain();
f.setPreviewAttach(this.boolean2int(this.getPreviewAttach()));        //寫入用戶部分!
try {
   UserInfo ui = this.getUserService().findUserInfoById(this.getUserSession().getId());
   if (ui == null) {
    this.addActionError(this.getText("error.post.guest"));
    this.setNeedsAttribute(false);
    return INPUT;
   }

   f = this.getForumService().createForum(f, this.getBoard(), ui, uploadFile);
   this.getUserCookie().addLastPostTime();
   if (this.getBoard().getAuditPost() == 0) {
    if (Constant.USE_URL_REWRITE) {
     this.setForwardUrl(this.getBasePath() + "forum-index-" + this.getBid() + "-" + this.getTagId()
       + "-1-0.html");
    } else {
     this.setForwardUrl(BBSCSUtil.getActionMappingURLWithoutPrefix("forum?action=index&bid="
       + this.getBid() + "&tagId=" + this.getTagId()));//forwardUrl用於發帖後成功的Href!
    }
   } else {
    this.setForwardUrl(BBSCSUtil.getActionMappingURLWithoutPrefix("postWaitAudit?bid=" + this.getBid()));
   }
   return SUCCESS;
}
好的,我們先轉看新投票功能http://bbs.laoer.com/votePost.bbscs?action=add&bid=15&tagId=0
<action name="votePost" class="votePostAction">
   <interceptor-ref name="boardInterceptorStack"></interceptor-ref>
   <interceptor-ref name="remoteAddrInterceptor"></interceptor-ref>
   <interceptor-ref name="requestBasePathInterceptor"></interceptor-ref>
   <result name="input">/WEB-INF/jsp/votePost.jsp</result>
   <result name="success" type="redirect">${forwardUrl}</result>
</action>
我們直接看VotePost:
public String add() {
this.setAction("addsave");
this.setDeadLineYear(String.valueOf(Util.getYear()));
/**
public static int getYear() {
Calendar cld = Calendar.getInstance();
cld.setTime(new java.util.Date());
return cld.get(Calendar.YEAR);
}
public static int getMonth() {
Calendar cld = Calendar.getInstance();
cld.setTime(new java.util.Date());
return cld.get(Calendar.MONTH) + 1;
}

*/
this.setDeadLineMon(Util.getMonth());
this.setTagsAttribute();
return INPUT;
}
另外,關於tagValues:
        <td width="85%" class="bgColor2">
    <s:if test="%{action=='addsave'}">
    <s:select list="tagValues" id="tagId" name="tagId" cssClass="select1" listKey="key" listValue="value"></s:select>
    </s:if>
    <s:textfield id="title" name="title" cssClass="input2" size="60" maxlength="60"></s:textfield>
        </td>
當點擊提交按鈕運行postSubmit()這個函數:
function postSubmit() {
document.getElementById("postButton").disabled = true;
document.votePost.submit();
}
在addSave()方法中:
f.setIsNew(1);
f.setIsTop(0);
f.setIsVote(1);
f.setLastPostNickName("---");
f.setLastPostTitle("");
f.setLastPostUserName("---");
f.setLastTime(nowtime);
f.setMainID("");
f.setNickName(this.getUserSession().getNickName());
f.setParentID("");
f.setPostTime(nowtime);
f.setPostType(0);
f.setQuoteText("");
f.setReNum(0);
f.setSign("");
f.setTitle(stitle);
f.setTitleColor(0);
f.setUserID(this.getUserSession().getId());
f.setUserName(this.getUserSession().getUserName());
f.setEmailInform(0);
f.setMsgInform(0);
...
Vote vote = new Vote();
vote.setDeadLine(Util.Date2Long(Integer.parseInt(this.getDeadLineYear()), this.getDeadLineMon(), this
    .getDeadLineDay()));
vote.setIsM(this.getIsM());
vote.setTitle(this.getTitle());
...
this.getForumService().createVoteForum(f, this.getBoard(), vote, ui, svoteItem);
this.getVoteService().saveVote(vote);
   this.setForwardUrl(this.getBasePath()
     + BBSCSUtil.getActionMappingURLWithoutPrefix("forum?action=index&bid=" + this.getBid()));//forwardUrl!!!
   return SUCCESS;
這裏用到了tagValues:
private void setTagsAttribute() {
this.tagValues.add(new OptionsString("0", this.getBoard().getBoardName()));
Iterator it = this.getBoard().getBoardTag().iterator();//這個Board的所有tag!
BoardTag bt = null;
while (it.hasNext()) {
   bt = (BoardTag) it.next();
   this.tagValues.add(new OptionsString(bt.getId(), bt.getTagName()));
}
}
OK!我們進入讀取帖子這個關鍵的業務,我們選擇一個喜歡的帖子進行分析(http://bbs.laoer.com/read-topic-15-ff80808113baa8140113d201333e5274-0-1-index-1.html),我們先在struts.xml中察看read這個action:
<action name="read" class="readAction">
   <interceptor-ref name="boardInterceptorStack"></interceptor-ref>
   <interceptor-ref name="remoteAddrInterceptor"></interceptor-ref>
   <interceptor-ref name="requestBasePathInterceptor"></interceptor-ref>
   <result name="read">/WEB-INF/jsp/read.jsp</result>
   <result name="readHistory">/WEB-INF/jsp/readHistory.jsp</result>
   <result name="postSummary">/WEB-INF/jsp/postSummary.jsp</result>
   <result name="postShowIpInfo">/WEB-INF/jsp/postShowIpInfo.jsp</result>
   <result name="showUpFileInPost">/WEB-INF/jsp/showUpFileInPost.jsp</result>
   <result name="showAttach">/WEB-INF/jsp/showAttach.jsp</result>
   <result name="showVoteInPost">/WEB-INF/jsp/showVoteInPost.jsp</result>
   <result name="readWaste">/WEB-INF/jsp/readWaste.jsp</result>
   <result name="readAuditing">/WEB-INF/jsp/readAuditing.jsp</result>
   <result name="readElite">/WEB-INF/jsp/readElite.jsp</result>
</action>
我們根據topic直接進入Read.java:
public String execute() { //可見其內容之豐富!
if (this.getAction().equalsIgnoreCase("topic")) {
   return this.topic();
} else if (this.getAction().equalsIgnoreCase("history")) {
   return this.history();
} else if (this.getAction().equalsIgnoreCase("own")) {
   return this.own();
} else if (this.getAction().equalsIgnoreCase("summary")) {
   return this.summary();
} else if (this.getAction().equalsIgnoreCase("showip")) {
   return this.showip();
} else if (this.getAction().equalsIgnoreCase("summaryhistory")) {
   return this.summaryhistory();
} else if (this.getAction().equalsIgnoreCase("showiphistory")) {
   return this.showiphistory();
} else if (this.getAction().equalsIgnoreCase("showupfile")) {
   return this.showupfile();
} else if (this.getAction().equalsIgnoreCase("attach")) {
   return this.attach();
} else if (this.getAction().equalsIgnoreCase("showvote")) {
   return this.showvote();
} else if (this.getAction().equalsIgnoreCase("waste")) {
   return this.waste();
} else if (this.getAction().equalsIgnoreCase("auditing")) {
   return this.auditing();
} else if (this.getAction().equalsIgnoreCase("auditingAttach")) {
   return this.auditingAttach();
} else if (this.getAction().equalsIgnoreCase("elite")) {
   return this.elite();
} else {
   return ERROR;
}
}
--->
public String topic() {
setUrlRewriteValue();//UrlRewrite!
Pages pages = new Pages();
pages.setPage(this.getInpages());
pages.setPerPageNum(this.getUserPostPerNum(this.getUserCookie().getPostPerNum(), this.getSysConfig().getPostPerPage()));//設置PrePageNum!
if (Constant.USE_URL_REWRITE) {
   pages.setUseUrlRewrite(Constant.USE_URL_REWRITE);
   pages.setFileName("read-" + this.getAction() + "-" + this.getBid() + "-" + this.getId() + "-"
     + this.getTagId() + "-" + this.getFcpage() + "-" + this.getFcaction() + "-{page}.html");
} else {
   pages.setFileName(this.getBasePath()
     + BBSCSUtil.getActionMappingURLWithoutPrefix("read?action=" + this.getAction() + "&bid="
       + this.getBid() + "&id=" + this.getId() + "&fcpage=" + this.getFcpage() + "&fcaction="
       + this.getFcaction() + "&tagId=" + this.getTagId()));
}
this.setPageList(this.getForumService().findForumsTopic(this.getBid(), this.getId(), pages));

if (this.getPageList().getObjectList().isEmpty()) {//沒有~~
   return this.history();
}

this.pageTitle = "";

Forum f = (Forum) this.getPageList().getObjectList().get(0);//主貼相關!
if (f.getIsNew() == 1) {
   f.setClick(f.getClick() + 1);
   try {
    f = this.getForumService().updateForum(f);
    if (f.getClick() == this.getSysConfig().getForumHotViews() && f.getIsHidden() == 0) {
     UserInfo ui = this.getUserService().findUserInfoById(f.getUserID());
     if (ui != null) {
      ui.setLiterary(ui.getLiterary() + 1); // 增加用戶文采值
      this.getUserService().saveUserInfo(ui);
     }
    }
   } catch (BbscsException ex) {
    this.addActionError(this.getText("error.post.getpost"));
    return ERROR;

   }
} else {
   if (this.getInpages() == 1) {//private int inpages = 1;

    this.addActionError(this.getText("error.post.getpost"));
    return ERROR;
   }
}
this.pageTitle = f.getTitle();
this.setTotalnum(this.getPageList().getPages().getTotalNum());
this.setParentBoards();
this.setForumSiteInit();
return "read";
}
我們注意最後的幾個set!
private int getUserPostPerNum(int userNum, int sysNum) {
if (userNum == 0) {
   return sysNum;
} else {
   return userNum;
}
}
private void setParentBoards() {
this.setParentBoards(this.getBoardService().findBoardsInIDs(this.getBoard().getParentIDs()));
}
private void setForumSiteInit() {
String url = this.getSysConfig().getForumUrl();
if (url == null) {
   url = "bbs.laoer.com";
} else if (url.startsWith("http://")) {
   url = url.substring("http://".length(), url.length());
}
this.setForumSite(url);
}
我們進入read.jsp:
<div id="head1right">   //頭部的right部分:[分頁: 1] [查看全部] [同主題打包] [訂閱] [返回]
      <div id="head1rcontent">

      [<s:text name="bbscs.pagebreak"/>: <bbscs:pages value="%{pageList.pages}" argPage="inpages" argTotal="topicTotal"/>]
<s:if test="%{action=='topic'}">
[<bbscs:topic type="own"/>]
</s:if>
<s:if test="%{action=='own'}">
[<bbscs:topic type="topic"/>]
</s:if>
        [<bbscs:topic type="mailsendtopic"/>]
        [<bbscs:topic type="subs" />]
        [<bbscs:topic type="returnforum"/>]

      </div>
    </div>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章