標籤:鬥醫 頁面緩衝 web基礎知識
本文把挑戰書的具體內容顯示在獨立頁面上,這裡涉及如下四個步驟:
1、增加觸發入口
2、增加業務配置
3、服務端擷取資料後封裝為Json對象返回
4、通過對DOM元素操作繪製頁面
在講解這四個步驟之前先修改一個Bug:
【問題描述】:
(1)使用者首次點擊“下戰書”菜單,進入登入介面提示使用者需要登入
(2)使用者登入成功之後就進入下戰書頁面
(3)此時再次點擊“下戰書”菜單,介面又進入到登入頁面
期望:下次再點擊“下戰書”菜單,此時的會話已存在,所以不應該再進入登入頁面
【問題原因】:
點擊“下戰書”菜單時,資料直接從瀏覽器記憶體中讀取,並沒有調用到服務端,所以這裡應該禁止直接從瀏覽器中直接讀取
【問題解決]:
解決辦法通過增加時間戳記的辦法來解決
(1)在common.js中增加兩個方法,分別用於響應“下戰書”和“首頁”菜單動作
/** * 響應首頁動作 */function doMainAction(){ top.location = "index.act?timestamp=" + new Date().getTime();}/** * 響應挑戰動作 */function doChallengeAction(){ top.location = "challenge.act?timestamp=" + new Date().getTime();}
(2)修改common.js中的generateSystemMenu()方法
systemMenu += ‘<li id="system_challenge_menu"><a href="javascript:doChallengeAction()">下戰書</a></li>‘;systemMenu += ‘<li id="system_home_menu"><a href="javascript:doMainAction()">首頁</a></li>‘;
通過這樣修改,點擊“下戰書”時瀏覽器地址欄中會出現時間戳記參數,如:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/47/E0/wKioL1QB_YKg1fq2AACEoP6C1Mw398.jpg" title="時間戳記.png" alt="wKioL1QB_YKg1fq2AACEoP6C1Mw398.jpg" />
接下來進入正題650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0010.gif" alt="j_0010.gif" />
1、增加觸發入口
入口就是《【鬥醫】【18】Web應用開發20天》講解的首頁上話題標題,所以需要修改main.js的操縱DOM把資料繪製到頁面方法appendData()
/** * 操縱DOM把資料繪製到頁面上 */function appendData(i, item){ // 略去部分內容 // >>>>>>設定挑戰話題標題 var topWrapper = $("<div />").attr("class", "main_item_wrapper"); var topicTitle = $("<div />").attr("class", "main_item_title"); var titleLink = $("<a />").attr("href", "javascript:forwardTopic(‘" + item.topicId + "‘)").text(item.topicTitle); titleLink.appendTo(topicTitle); topicTitle.appendTo(topWrapper); // 略去部分內容}
在main.js中增加forwardTopic()方法
/** * 跳轉到話題內容 */function forwardTopic(topicId){ top.location = "topic.act?topicId=" + topicId + "×tamp=" + new Date().getTime();}
2、增加業務配置
(1)為了能響應topic.act請求,需要在system-action.xml中增加業務配置
<business name="topic" mustlogin="false"> <forward> <path name="success" path="/module/topic/topic.html" /> <path name="failure" path="/module/topic/topic.html" /> </forward></business>
(2)業務配置完後經過系統轉寄後跳轉到topic.html頁面,所以需要在D:\medical\war\module\topic下增加topic.html檔案
<!DOCTYPE HTML><html> <head> <title>鬥醫</title> <!--利於搜尋引擎查詢--> <meta name="description" content="鬥醫是一個醫學交流平台" /> <meta name="keywords" content="醫學,交流,討論" /> <!--設定字元集--> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <!--頁面不緩衝--> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache,must-revalidate" /> <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT" /> <!--引入外部檔案--> <link rel="stylesheet" type="text/css" href="theme/navigation/navigation.css"> <link rel="stylesheet" type="text/css" href="theme/topic/topic.css"> <script src="js/common/jquery.js"></script> <script src="js/common/common.js"></script> <script src="js/topic/topic.js"></script> </head> <body> <!--系統導覽功能表--> <div id="system_navigation_menu"></div> <!--系統內容部分--> <div class="system_content"> <div class="system_content_left"> <div class="topic_content_wrapper" id="topic_content_dynamic_id"> </div> </div> <div class="system_content_right"></div> </div> </body></html>
(3)由於topic.html檔案中需要引用topic.js和topic.css,所以在系統中增加這兩個檔案。
(4)當topic.js載入時通過Ajax非同步呼叫到服務端
(function( window){ $(document).ready(function() { // 產生系統功能表 generateSystemMenu(); // 設定"首頁"菜單選中 selectSystemMenu("system_home_menu"); // 擷取話題內容 getTopicContent(); }); /** * 擷取話題內容 */ function getTopicContent() { var data = {"topicId": getTopicId()}; asyncRequest("topicContent.data", data, function(result) { }); }})( window );
3、服務端擷取資料後封裝為Json對象返回
(1)由於使通過topicContent.data調用到服務端,所以需要配置如下內容
<!--擷取話題內容資訊--><business name="topicContent" business-class="com.medical.server.data.TopicDataAction" />
(2)定義TopicDataAction類,完成服務端資料讀取和封裝
/** * 鬥醫系統擷取科室資訊處理類 * * @author qingkechina 2014-08-31 */public class TopicDataAction extends FrameDefaultAction{ /** * 全域Gson對象 */ private final static Gson gson = new Gson(); @Override public String execute() throws FrameException { // 擷取目前記錄索引值 String topicId = getParameter("topicId"); if (FrameUtil.isEmpty(topicId)) { FrameResultBean result = new FrameResultBean(); result.setErrorCode(FrameErrorCode.REQUEST_PARAM_ERROR); result.setErrorDesc(FrameUtil.getErrorDescByCode(result.getErrorCode())); return gson.toJson(result); } // 擷取不到話題 TopicDAO topic = TopicUtil.getTopicById(topicId); if (topic == null) { FrameResultBean result = new FrameResultBean(); result.setErrorCode(FrameErrorCode.REQUEST_CONTENT_EMPTY); result.setErrorDesc(FrameUtil.getErrorDescByCode(result.getErrorCode())); return gson.toJson(result); } return gson.toJson(topic); }}
4、通過對DOM元素操作繪製頁面
完善topic.js檔案中的getTopicContent()方法
/** * 擷取話題內容 */function getTopicContent(){ var data = {"topicId": getTopicId()}; asyncRequest("topicContent.data", data, function(result) { // 若有錯誤碼返回表明擷取資料異常 var resultJson = eval(result); if(resultJson.errorCode) { showSystemGlobalInfo(resultJson.errorDesc); return; } // 操縱DOM在頁面繪製資料 appendTopic2Page(resultJson); });}
/** * 操縱DOM在頁面繪製資料 */function appendTopic2Page(result){ // 添加標題 var contentId = $("#topic_content_dynamic_id"); var topicTitle = $("<h2 />").text(result.topicTitle); topicTitle.appendTo(contentId); // 新增內容 var topicContent = $("<div />").attr("class", "topic_content").html(result.prescript); topicContent.appendTo(contentId); // 添加附屬內容 var topicAttach = $("<div />").attr("class", "topic_attach"); var attachPanel = $("<div />").attr("class", "topic_attach_panel"); attachPanel.appendTo(topicAttach); topicAttach.appendTo(contentId); // 添加話題評論 var commentIcon = $("<i />").attr("class", "topic_comment_icon"); var commentLink = $("<a />").attr("href", "#").attr("class", "topic_comment_link"); commentIcon.appendTo(commentLink); commentLink.text("添加評論"); commentLink.appendTo(attachPanel); // 添加話題分享 var shareIcon = $("<i />").attr("class", "topic_share_icon"); var shareLink = $("<a />").attr("href", "#").attr("class", "topic_share_link"); shareIcon.appendTo(shareLink); shareLink.text("分享"); shareLink.appendTo(attachPanel); // 添加話題邀請 var inviteLink = $("<a />").attr("href", "#").attr("class", "topic_invite_link"); inviteLink.text("邀請回答"); inviteLink.appendTo(attachPanel); // 添加話題舉報 var reportIcon = $("<i />").attr("class", "topic_report_icon"); var reportLink = $("<a />").attr("href", "#").attr("class", "topic_report_link"); reportIcon.appendTo(reportLink); reportLink.text("舉報"); reportLink.appendTo(attachPanel);}
在瀏覽器中輸入http://localhost:8080/medical,斷行符號後如下:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/47/E6/wKiom1QDTm7gtBLsAAIb1L5g9tU056.jpg" title=".png" alt="wKiom1QDTm7gtBLsAAIb1L5g9tU056.jpg" />
【備忘】:這裡面還有很多功能待完善,如評論功能、分享功能、邀請回答功能以及右側內容等,這些功能要留到JSP內容部分實現了650) this.width=650;" src="http://img.baidu.com/hi/tsj/t_0015.gif" alt="t_0015.gif" />
本文出自 “青客” 部落格,請務必保留此出處http://qingkechina.blog.51cto.com/5552198/1547236
【鬥醫】【19】Web應用開發20天