您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Struts2 中json插件如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
***步:在struts2基本jar包的基礎(chǔ)上添加struts2-jsonplugin
第二步:在stuts.xml文件中將extends由默認的struts-default改為json-default
<package name="tutorial" extends="json-default">
第三步:在jsp中引入jquery
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
第四步:畫頁面
<%@ 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">專業(yè)動態(tài)</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"> <h2> <a id="tit" class="tit" href="" target="_blank" mon="a=7">${ins.b_title }</a> </h2> <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"> 發(fā)表評論 </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"> Copyright (c) 2012 Inc All rights reserved 版權(quán)所有 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> 國家安全生產(chǎn)監(jiān)督管理總局 </option> <option value=http://www.hljmj.gov.cn /> 省煤礦安全監(jiān)察局 </option> <option value=http://www.chinacoal.org.cn /> 中國煤炭工業(yè)網(wǎng) </option> <option value=http://www.triz.gov.cn /> 技術(shù)創(chuàng)新方法 </option> <option value=http://www.cumt.edu.cn /> 中國礦業(yè)大學 </option> <option value=http://www.triz.gov.cn /> 中國礦業(yè)大學北京校區(qū) </option> </select> </div> <br /> <br /> </body> </html>
第四步:寫業(yè)務(wù)代碼
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(); } }
第五步:在action調(diào)用該方法:目的是將留言錄入數(shù)據(jù)庫
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; } }
看完上述內(nèi)容,你們對Struts2 中json插件如何使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。