Struts2 json插件的使用

废了一天的功夫,终于把我的留言功能实现了.有时候一个纠结的问题,会耽误自己很长时间,如何提高开发效率,是个很大的问题!详细请看下文
首页 新闻资讯 行业资讯 Struts2 json插件的使用

废了一天的功夫,终于把我的留言功能实现了.有时候一个纠结的问题,会耽误自己很长时间,如何提高开发效率,是个很大的问题!

总结一下今天的内容:

我要实现的功能是,在一篇文章的***,添加一个留言板块,在留言之后能够直接显示在页面上

***步:在struts2基本jar包的基础上添加struts2-jsonplugin

第二步:在stuts.xml文件中将extends由默认的struts-default改为json-default

复制

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

第三步:在jsp中引入jquery

复制

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

第四步:画页面

复制

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  <%      String path = request.getContextPath();      String basePath = request.getScheme() + "://"             + request.getServerName() + ":" + request.getServerPort()              + path + "/";  %>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html>      <head>          <title>新闻公告</title>           <link href="css/register.css" rel="stylesheet" type="text/css" />          <script type="text/javascript" src="js/jquery-1.7.2.js"></script>          <script type="text/javascript">          jQuery(document).ready(function()         {                  $("#mes_button").bind("click",function(event){                                    var m_name = $("#m_name").val();                  var m_content = $("#m_content").val();                  var b_id = $("#b_id").val();                  $.ajax({                      type: "POST",                      url: "addMessage.action",                      data:{"m_name":m_name,"m_content":m_content,"b_id":b_id} ,                      success: function (data) {                                 $("#message_list").append("<div class='message_box' id=''><div class='message_text'><strong>"                       + data.m_name + "</strong><p>" + data.m_content +"</p></div></div>");                      }                  });                             });                           });            </script>      </head>       <body>          <div id="main">              <div id="head">                   <div class="head_bg">                       <div class="head_menu">                           <ul>                              <div class="head_left">                                  <a href="http://www.cnblogs.com/index.jsp">首页</a>                              </div>                               <div class="head_right">                                  <a href="jsp/news/news.jsp">新闻公告</a>                              </div>                               <div class="head_right">                                  <a href="jsp/news/news.jsp">专业动态</a>                              </div>                               <div class="head_right">                                  <a href="">文章列表</a>                              </div>                               <div class="head_right">                                  <a href="jsp/news/news.jsp">成果展示</a>                              </div>                                <div class="head_right">                                  <a href="">下载中心</a>                              </div>                               <div class="head_right">                                  <a href="">管理员入口</a>                              </div>                           </ul>                      </div>                  </div>              </div>               <div class="underhead">                  <span class="l"> 正文 </span>              </div>               <div align="left" style="width: 100%">                  <div class="news">                      <div class="newscontent">                          <h1>                              <a id="tit" class="tit" href="" target="_blank" mon="a=7">${ins.b_title }</a>                          </h1>                          <div class="overcontent">                              时间:${ins.b_date }                          </div>                          <div id="content">                              ${ins.b_content}                           </div>                          <div id="undercontent">                              <input id="b_id" type="hidden" name="b_id" value="8">                          </div>                      </div>                  </div>                  <div id="message_list">                   </div>                       <div class="message">                          <div class="message_title">                              发表评论                          </div>                          <div class="message_user">                              用户名:                              <input disabled="disabled" id="m_name"                                 type="text" value="houjinxin" name="m_name"/>                          </div>                          <form action="">                              <div class="message_content">                                  <textarea id="m_content" rows="5" cols="80" name="m_content"></textarea>                              </div>                              <div class="message_button">                                  <input id="mes_button" type="button" value="提交评论" />                              </div>                                                        </form>                      </div>              </div>              </div>              <div>                  <br/>              </div>                            <div id="foot">                  &nbsp;&nbsp; Copyright (c) 2012 Inc All rights reserved 版权所有 by                  黑龙江科技学院                   <select>                      <option value="" selected>                          ----------友情链接----------                      </option>                      <option value=http://www.moe.edu.cn />                          国家教育部                      </option>                      <option value=http://www.pgzx.edu.cn />                          教育教学评估中心                      </option>                      <option value=http://www.hlje.net />                          省教育厅                      </option>                      <option value=http://www.chinasafety.gov.cn/index.htm>                          国家安全生产监督管理总局                      </option>                      <option value=http://www.hljmj.gov.cn />                          省煤矿安全监察局                      </option>                      <option value=http://www.chinacoal.org.cn />                           中国煤炭工业网                      </option>                      <option value=http://www.triz.gov.cn />                          技术创新方法                      </option>                      <option value=http://www.cumt.edu.cn />                          中国矿业大学                      </option>                      <option value=http://www.triz.gov.cn />                          中国矿业大学北京校区                      </option>                  </select>              </div>              <br />              <br />      </body>   </html>
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.

  • 137.

  • 138.

  • 139.

  • 140.

  • 141.

  • 142.

  • 143.

  • 144.

  • 145.

  • 146.

  • 147.

  • 148.

  • 149.

  • 150.

  • 151.

  • 152.

  • 153.

  • 154.

  • 155.

  • 156.

  • 157.

  • 158.

  • 159.

  • 160.

  • 161.

  • 162.

  • 163.

  • 164.

  • 165.

  • 166.

  • 167.

  • 168.

  • 169.

  • 170.

  • 171.

  • 172.

  • 173.

  • 174.

  • 175.

第四步:写业务代码

复制

public void addMessage(int b_id,String m_content,String m_name)      {          PreparedStatement ps = null;          try         {              String sql = "insert into message(b_id,m_name,m_content,m_date,m_status) values(?,?,?,?,?)";               ps = DBUtils.getConnection().prepareStatement(sql);              ps.setInt(1,b_id);              ps.setString(2,m_content);              ps.setString(3,m_name );              ps.setDate(4, new Date(new java.util.Date().getTime()));              ps.setString(5, "1");              ps.executeUpdate();          }          catch (Exception e)          {              e.printStackTrace();          }          finally         {              DBUtils.close(ps);              DBUtils.close();          }                }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

第五步:在action调用该方法:目的是将留言录入数据库

复制

package com.ele.web.action.message;   import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream;  import java.io.PrintWriter;  import java.text.DateFormat;  import java.text.SimpleDateFormat;  import java.sql.Date;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;   import org.apache.struts2.ServletActionContext;   import com.ele.services.MessageServices;  import com.ele.services.NewsServices;  import com.ele.vo.Ele_messageVO;  import com.ele.vo.Ele_newsVO;  import com.opensymphony.xwork2.ActionContext;  import com.opensymphony.xwork2.ActionSupport;   public class AddMessageAction extends ActionSupport  {      private String m_name;            private String m_content;            private int b_id;            public int getB_id()      {          return b_id;      }       public void setB_id(int bId)      {          b_id = bId;      }       public String getM_name()      {          return m_name;      }       public void setM_name(String mName)      {          m_name = mName;      }       public String getM_content()      {          return m_content;      }       public void setM_content(String mContent)      {          m_content = mContent;      }            @Override     public String execute()      {           MessageServices msgServices = new MessageServices();          msgServices.addMessage(b_id, m_content, m_name);                  return SUCCESS;      }        }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

到目前为止,在点击提交按钮后,数据会被显示到页面上,但是刷新之后就没有了.这也是我将要完成的部分。

我设想的解决方法是在查询文章的时候,将留言一并查出,不知道还会遇到什么问题。

在今天的工作过程中,最让我纠结的问题就是,利用ajax异步传值和响应数据处理这部分。

试了无数种方法都无果.***还是在别人的帮助下完成的.留下此文,记录学习过程。

原文链接:http://www.cnblogs.com/houjinxin/archive/2012/05/07/2488224.html

【编辑推荐】

  1. Java简单的网络爬虫实现

  2. Java调用SQL Server的存储过程详解

  3. MongoDB、Java与对象关系映射

  4. Java的Comparable接口的一个陷阱

  5. Java程序设计:图形与多媒体处理

27    2012-05-10 14:00:06    Struts json Java