【正文】
indowprompt 請(qǐng)輸入要以下劃線顯示的文字 if text text1 [_[u]_]text[_[u]_] if text null newtopice newtopicetext1 上述代碼將用戶輸入主題內(nèi)容以 [_[u]_]text[_[u]_]樣式傳入ActionForm Bean 中然后提交到 Action 控制器中將數(shù)據(jù)插入數(shù)據(jù)庫(kù)中當(dāng)用戶從數(shù)據(jù)庫(kù)中取這些數(shù)據(jù)時(shí) [_[u]_]不是有效的 HTML 代碼此時(shí)需要對(duì)這個(gè)字符串進(jìn)行轉(zhuǎn)換操作用到輔助 hangejava 關(guān)鍵代碼如下 public class umlChange public String Change String str String s strreplaceAll \\[\\_\\[ replaceAll \\]\\_\\] replaceAll \\\\_\\ return strim 創(chuàng)建控制器組件 用戶發(fā)布主題模塊的控制器組件主要包括如下兩個(gè)部分 1 用于轉(zhuǎn)入發(fā)布主題頁(yè)面 NewtopicActionjava 控制器組件 在 strutsconfigxml 配置信息中 newTstrutsaction NewTopicActionjava 控制器同時(shí)發(fā)送到這個(gè) Action 控制器的參數(shù)包括typenameforumidforumname 其中參數(shù) type 標(biāo)識(shí)用戶發(fā)表回復(fù)文章還是發(fā)表新主題參數(shù) name 為當(dāng)前登陸的用戶名參數(shù) forumid 為論壇號(hào)參數(shù) forumname 為論壇名稱 同時(shí)還需要取出發(fā)表文章作者相關(guān)信息數(shù)據(jù)取出后以 List 形式表示這些參數(shù)通過(guò) session傳入 newarticlejsp文件中待發(fā)送 newAticledo請(qǐng)求時(shí)將這些數(shù)據(jù)提交到相應(yīng)的 Action控制器中然后根據(jù) type的值來(lái)確定調(diào)用哪個(gè)業(yè)務(wù)邏輯類處理這些數(shù)據(jù)關(guān)鍵代碼如下 public ActionForward execute ActionMapping mapping ActionForm form HttpServletRequest request HttpServletResponse response DataSource datasource getDataSource requestdataSources 取 struts 配置的數(shù)據(jù)源 NewtopicForm newtopicForm NewtopicForm form TODO Autogenerated method stub ActionMessages error new ActionMessages HttpSession session requestgetSession String name newtopicFormgetName String forumid newtopicFormgetForumid String forumname null if forumid null forumid String sessiongetAttribute forumId ForumBean forum new ForumBean List forumnlist forumgetRecord datasource forumid for int i 0i forumnlistsize i ForumBean forums ForumBean forumnlistget i forumname forumsgetForumnname String topiame String sessiongetAttribute title Sytln topiame Sytln name Sytln forumnameforumname Sytln forumidforumid sessionsetAttribute name name sessionsetAttribute forumid forumid 將用戶名與論壇號(hào)以及論壇名稱傳入發(fā)表新主題頁(yè)面 sessionsetAttribute forumname forumname String type newtopicFormgetType Sytln typetype sessionsetAttribute type type LoginBean login new LoginBean List list logingetRecordForUser name datasource 取出這個(gè)用戶所有相關(guān)信息 sessionsetAttribute listuserlist return mappinggetInputForward 2 用于提交主題表單的控制器組件 NewAticleActionjava 控制器 在 strutsconfigxml配置信息中 newAtstticleActionjava控制器在控制器中根據(jù) type 的值調(diào)用不同的業(yè)務(wù)邏輯處理類關(guān)鍵代碼如下 public ActionForward execute ActionMapping mapping ActionForm form HttpServletRequest request HttpServletResponse response TODO Autogenerated method stub int i 0 判斷是否插入成功的變量 i int j 0 判斷將數(shù)據(jù)插入 tb_response 表中是否成功 List listresponsetitle null 取回復(fù)用戶的相關(guān)信息 NewtopicForm newtopicForm NewtopicForm form ActionMessages error new ActionMessages HttpSession session requestgetSession StringTrans s new StringTrans String title newtopicFormgetTitle String content newtopicFormgetContent String xq newtopicFormgetXq String name String sessiongetAttribute name String forumid String sessiongetAttribute forumId String forumname String sessiongetAttribute forumName String type String sessiongetAttribute type String topicid String sessiongetAttribute topicid String topiame String sessiongetAttribute title Sytln typetype Sytln topiameforumname TopicListBean t new TopicListBean DataSource datasource getDataSource requestdataSources 取 struts 配置的數(shù)據(jù)源 if type null i tInsertData title content xq name forumid forumname datasource 如果是發(fā)表主題 tb_topic 表中 Sytln 插入主題結(jié)果 i else LoginBean l new LoginBean j tInsertDataResponse title content xq name topicidtopiamedatasource 如果是回復(fù)主題 tb_response 表中 listresponsetitle lgetRecordForUser name datasource 取出此作者的相關(guān)信息 Sytln jj if i 1 try List l tsearch datasource 0 forumid 查詢?cè)撜搲闹黝} sessionsetAttribute List l Sytln new atrticleactionl catch Exception e eprintStackTrace return mappingfindForward success if j 1 sessionsetAttribute listreponsetitle listresponsetitle TopicListBean topic new TopicListBean List listResponse List topicgetRecordResponse topicid datasource sessionsetAttribute listResponse listResponse return mappingfindForward responsesuccess return mappinggetInputForward 創(chuàng)建業(yè)務(wù)輔助類模型組件 在發(fā)布主題模塊中用到了 3個(gè)業(yè)務(wù)輔助類其中 umlChangejava已經(jīng)在視圖組件中說(shuō)明除此之外還包括一下兩個(gè)業(yè)務(wù)輔助類 1 TopicListBeanjava 類 cListBeanjava 業(yè)務(wù)輔助類中的 InsertData 方法與InsertDataResponse 方法分別在 NewAticleActionjava 控制器中用于將數(shù)據(jù)插入主題表和回復(fù)表關(guān)鍵代碼如 下 public int InsertData String titleString contentString xqString nameString forumidString forumnameDataSource datasource int i 0 DB d new DB datasource PreparedStatement pstmt null String sql insert into tb_topic titlecontentxqauthorforumidforumname values pstmt dPrepareInsert sql try pstmtsetString 1title pstmtsetString 2content pstmtsetString 3xq pstmtsetString 4name pstmtsetString 5forumid pstmtsetString 6 forumname i pstmtexecuteUpdate catch SQLException e eprintStackTrace dclose return i 2 LoginBeanjava 類 在很多 Action 控制器中都用到了 LoginBeanjava 業(yè)務(wù)輔助類因?yàn)樗敲枋鲇脩粝嚓P(guān)信息的業(yè)務(wù)輔助類在 NewtopicActionjava 控制器中用到了這個(gè)業(yè)務(wù)