温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Struts2 中json插件如何使用

发布时间:2021-08-12 16:24:11 来源:亿速云 阅读:131 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关Struts2 中json插件如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

***步:在struts2基本jar包的基础上添加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">专业动态</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">                              发表评论                          </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>

第四步:写业务代码

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调用该方法:目的是将留言录入数据库

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;      }        }

看完上述内容,你们对Struts2 中json插件如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI