Struts2 json插件的使用

廢了一天的功夫,終於把我的留言功能實現了.有時候一個糾結的問題,會耽誤自己很長時間,如何提高開發效率,是個很大的問題!

總結一下今天的內容:

我要實現的功能是,在一篇文章的最後,添加一個留言板塊,在留言之後能夠直接顯示在頁面上

第一步:在struts2基本jar包的基礎上添加struts2-jsonplugin

第二步:在stuts.xml文件中將extends由默認的struts-default改爲json-default

  1. <package name="tutorial" extends="json-default"> 

第三步:在jsp中引入jquery

  1. <script type="text/javascript" src="js/jquery-1.7.2.js"></script> 

第四步:畫頁面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://" 
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  9. <html>  
  10.     <head>  
  11.         <title>新聞公告</title>  
  12.  
  13.         <link href="css/register.css" rel="stylesheet" type="text/css" />  
  14.         <script type="text/javascript" src="js/jquery-1.7.2.js"></script>  
  15.         <script type="text/javascript">  
  16.         jQuery(document).ready(function()  
  17.        {  
  18.                 $("#mes_button").bind("click",function(event){  
  19.                   
  20.                 var m_name = $("#m_name").val();  
  21.                 var m_content = $("#m_content").val();  
  22.                 var b_id = $("#b_id").val();  
  23.                 $.ajax({  
  24.                     type: "POST",  
  25.                     url: "addMessage.action",  
  26.                     data:{"m_name":m_name,"m_content":m_content,"b_id":b_id} ,  
  27.                     success: function (data) {  
  28.                                $("#message_list").append("<div class='message_box' id=''><div class='message_text'><strong>"   
  29. + data.m_name + "</strong><p>" + data.m_content +"</p></div></div>");  
  30.                     }  
  31.                 });  
  32.                   
  33.          });  
  34.                  
  35.         });   
  36.  
  37.         </script>  
  38.     </head>  
  39.  
  40.     <body>  
  41.         <div id="main">  
  42.             <div id="head">  
  43.  
  44.                 <div class="head_bg">  
  45.  
  46.                     <div class="head_menu">  
  47.  
  48.                         <ul>  
  49.                             <div class="head_left">  
  50.                                 <a href="http://www.cnblogs.com/index.jsp">首頁</a>  
  51.                             </div>  
  52.  
  53.                             <div class="head_right">  
  54.                                 <a href="jsp/news/news.jsp">新聞公告</a>  
  55.                             </div>  
  56.  
  57.                             <div class="head_right">  
  58.                                 <a href="jsp/news/news.jsp">專業動態</a>  
  59.                             </div>  
  60.  
  61.                             <div class="head_right">  
  62.                                 <a href="">文章列表</a>  
  63.                             </div>  
  64.  
  65.                             <div class="head_right">  
  66.                                 <a href="jsp/news/news.jsp">成果展示</a>  
  67.                             </div>  
  68.  
  69.  
  70.                             <div class="head_right">  
  71.                                 <a href="">下載中心</a>  
  72.                             </div>  
  73.  
  74.                             <div class="head_right">  
  75.                                 <a href="">管理員入口</a>  
  76.                             </div>  
  77.  
  78.                         </ul>  
  79.                     </div>  
  80.                 </div>  
  81.             </div>  
  82.  
  83.             <div class="underhead">  
  84.                 <span class="l"> 正文 </span>  
  85.             </div>  
  86.  
  87.             <div align="left" style="width: 100%">  
  88.                 <div class="news">  
  89.                     <div class="newscontent">  
  90.                         <h1>  
  91.                             <a id="tit" class="tit" href="" target="_blank" mon="a=7">${ins.b_title }</a>  
  92.                         </h1>  
  93.                         <div class="overcontent">  
  94.                             時間:${ins.b_date }  
  95.                         </div>  
  96.                         <div id="content">  
  97.                             ${ins.b_content}   
  98.                         </div>  
  99.                         <div id="undercontent">  
  100.                             <input id="b_id" type="hidden" name="b_id" value="8">  
  101.                         </div>  
  102.                     </div>  
  103.                 </div>  
  104.                 <div id="message_list">  
  105.  
  106.                 </div>  
  107.  
  108.                     <div class="message">  
  109.                         <div class="message_title">  
  110.                             發表評論  
  111.                         </div>  
  112.                         <div class="message_user">  
  113.                             用戶名:  
  114.                             <input disabled="disabled" id="m_name" 
  115.                                 type="text" value="houjinxin" name="m_name"/>  
  116.                         </div>  
  117.                         <form action="">  
  118.                             <div class="message_content">  
  119.                                 <textarea id="m_content" rows="5" cols="80" name="m_content"></textarea>  
  120.                             </div>  
  121.                             <div class="message_button">  
  122.                                 <input id="mes_button" type="button" value="提交評論" />  
  123.                             </div>  
  124.                               
  125.                         </form>  
  126.                     </div>  
  127.             </div>  
  128.             </div>  
  129.             <div>  
  130.                 <br/>  
  131.             </div>  
  132.               
  133.             <div id="foot">  
  134.                 &nbsp;&nbsp; Copyright (c) 2012 Inc All rights reserved 版權所有 by  
  135.                 黑龍江科技學院  
  136.  
  137.                 <select>  
  138.                     <option value="" selected>  
  139.                         ----------友情鏈接----------  
  140.                     </option>  
  141.                     <option value=http://www.moe.edu.cn />  
  142.                         國家教育部  
  143.                     </option>  
  144.                     <option value=http://www.pgzx.edu.cn />  
  145.                         教育教學評估中心  
  146.                     </option>  
  147.                     <option value=http://www.hlje.net />  
  148.                         省教育廳  
  149.                     </option>  
  150.                     <option value=http://www.chinasafety.gov.cn/index.htm>  
  151.                         國家安全生產監督管理總局  
  152.                     </option>  
  153.                     <option value=http://www.hljmj.gov.cn />  
  154.                         省煤礦安全監察局  
  155.                     </option>  
  156.                     <option value=http://www.chinacoal.org.cn />  
  157.  
  158.                         中國煤炭工業網  
  159.                     </option>  
  160.                     <option value=http://www.triz.gov.cn />  
  161.                         技術創新方法  
  162.                     </option>  
  163.                     <option value=http://www.cumt.edu.cn />  
  164.                         中國礦業大學  
  165.                     </option>  
  166.                     <option value=http://www.triz.gov.cn />  
  167.                         中國礦業大學北京校區  
  168.                     </option>  
  169.                 </select>  
  170.             </div>  
  171.             <br />  
  172.             <br />  
  173.     </body>  
  174.  
  175. </html> 

第四步:寫業務代碼

  1. public void addMessage(int b_id,String m_content,String m_name)  
  2.     {  
  3.         PreparedStatement ps = null;  
  4.         try 
  5.         {  
  6.             String sql = "insert into message(b_id,m_name,m_content,m_date,m_status) values(?,?,?,?,?)";   
  7.             ps = DBUtils.getConnection().prepareStatement(sql);  
  8.             ps.setInt(1,b_id);  
  9.             ps.setString(2,m_content);  
  10.             ps.setString(3,m_name );  
  11.             ps.setDate(4new Date(new java.util.Date().getTime()));  
  12.             ps.setString(5"1");  
  13.             ps.executeUpdate();  
  14.         }  
  15.         catch (Exception e)  
  16.         {  
  17.             e.printStackTrace();  
  18.         }  
  19.         finally 
  20.         {  
  21.             DBUtils.close(ps);  
  22.             DBUtils.close();  
  23.         }  
  24.           
  25.     } 

第五步:在action調用該方法:目的是將留言錄入數據庫

  1. package com.ele.web.action.message;  
  2.  
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.OutputStream;  
  11. import java.io.PrintWriter;  
  12. import java.text.DateFormat;  
  13. import java.text.SimpleDateFormat;  
  14. import java.sql.Date;  
  15. import javax.servlet.http.HttpServletRequest;  
  16. import javax.servlet.http.HttpServletResponse;  
  17.  
  18. import org.apache.struts2.ServletActionContext;  
  19.  
  20. import com.ele.services.MessageServices;  
  21. import com.ele.services.NewsServices;  
  22. import com.ele.vo.Ele_messageVO;  
  23. import com.ele.vo.Ele_newsVO;  
  24. import com.opensymphony.xwork2.ActionContext;  
  25. import com.opensymphony.xwork2.ActionSupport;  
  26.  
  27. public class AddMessageAction extends ActionSupport  
  28. {  
  29.     private String m_name;  
  30.       
  31.     private String m_content;  
  32.       
  33.     private int b_id;  
  34.       
  35.     public int getB_id()  
  36.     {  
  37.         return b_id;  
  38.     }  
  39.  
  40.     public void setB_id(int bId)  
  41.     {  
  42.         b_id = bId;  
  43.     }  
  44.  
  45.     public String getM_name()  
  46.     {  
  47.         return m_name;  
  48.     }  
  49.  
  50.     public void setM_name(String mName)  
  51.     {  
  52.         m_name = mName;  
  53.     }  
  54.  
  55.     public String getM_content()  
  56.     {  
  57.         return m_content;  
  58.     }  
  59.  
  60.     public void setM_content(String mContent)  
  61.     {  
  62.         m_content = mContent;  
  63.     }  
  64.       
  65.     @Override 
  66.     public String execute()  
  67.     {  
  68.  
  69.         MessageServices msgServices = new MessageServices();  
  70.         msgServices.addMessage(b_id, m_content, m_name);  
  71.                 return SUCCESS;  
  72.     }  
  73.       

到目前爲止,在點擊提交按鈕後,數據會被顯示到頁面上,但是刷新之後就沒有了.這也是我將要完成的部分。

我設想的解決方法是在查詢文章的時候,將留言一併查出,不知道還會遇到什麼問題。

在今天的工作過程中,最讓我糾結的問題就是,利用ajax異步傳值和響應數據處理這部分。

試了無數種方法都無果.最後還是在別人的幫助下完成的.留下此文,記錄學習過程。

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