ACTION

package actions;

import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import pojo.Goods;
import service.GoodsService; public class GoodsAction extends ActionSupport { //定义msg数组传值到ajax
private String []msg = new String[]{"null","null","null","null","null","null"}; public String[] getMsg() {
return msg;
} public void setMsg(String[] msg) {
this.msg = msg;
} /**
* 查询出返回主界面
* 将json转string
* @return 返回string msg数组
* @throws Exception
*/
public String showmygoods() throws Exception{ // String str = "9787121321450";
// long id = Long.parseLong(str);
// goods.setGoodsId(id); goods=goodsService.find_goods(goods.getGoodsId()); if(goods !=null) { try {
JSONObject jsonObject = JSONObject.fromObject(goods); msg[0] = jsonObject.getString("goodsId");
msg[1] = jsonObject.getString("goodsName");
msg[2] = jsonObject.getString("goodsType");
msg[3] = jsonObject.getString("goodsPrice");
msg[4] = jsonObject.getString("goodsFrom");
msg[5] = jsonObject.getString("goodsAmount"); //json获取指定键值数据 字符串进行拼接
// msg = jsonObject.getString("goodsId")+","+jsonObject.getString("goodsName")+","+
// jsonObject.getString("goodsType")+","+jsonObject.getString("goodsPrice")+","+
// jsonObject.getString("goodsFrom")+","+jsonObject.getString("goodsAmount");
// System.out.println(msg);
goods=null;
} catch (Exception e) {
e.printStackTrace();
}
} return SUCCESS;
}

STRUTS.XML


<!-- jquery,json方式 -->
<action name="showmygoods" method="showmygoods" class="goodsAction">
<result type="json">
<!--是否去掉null值,默认为false-->
<param name="excludeNullProperties">true</param>
<param name="root">msg</param>
</result>
<!--<result name="success" type="json"></result>-->
</action>

JSP

  <script type="text/javascript" src="assets/js/jquery-1.10.2.js"></script>

    <script>
<%--jax+struts2+JQuery+json--%>
$(document).ready(function() {
$("#ajaxA").click(function() {
// var obj = $('#rate').prop("value");
$.ajax({
type : 'post',//请求方式
url : 'showmygoods.action',//请求路径
// data : {//传给action的参数,rate在action中必须有get,set方法
// goodsid : obj
// },
// dataType : 'json',//以json格式封装数据
//无异常时执行的方法
success : function(msg) { // var html = ''; //
// html = html + '<thead>';
// html = html + '<tr>';
// html = html + '<th>商品编号</th>';
// html = html + '<th>商品名称</th>';
// html = html + '<th>商品分流</th>';
// html = html + '<th>商品价格(元)</th>';
// html = html + '<th>商品产地</th>';
// html = html + '<th>商品数量(件)</th>';
// html = html + '</tr>';
// html = html + '</thead>';
// $("#mytable").html(html); if (!(msg[0]=='null')) { //判断数组非空 // html = html + '<tr>';
// html = html + '<td>' + msg[0] + '</td>';
// html = html + '<td>' + msg[1] + '</td>';
// html = html + '<td>' + msg[2] + '</td>';
// html = html + '<td>' + msg[3] + '</td>';
// html = html + '<td>' + msg[4] + '</td>';
// html = html + '<td>' + msg[5] + '</td>';
// html = html + '</tr>'; var addRow = "<tr><td>"+msg[0]+"</td><td>"+msg[1]+"</td><td>"+msg[2]+"</td><td>"+
msg[3]+"</td><td>"+msg[4]+"</td><td>"+msg[5]+"</td></tr>"
$("#mytable tbody").append(addRow);
} },
//出现异常时执行的方法
error : function(data) {
$("#mytable").html("出现异常");
}
});
});
});
</script> </head>
<body> <button id="ajaxA">ajax刷新</button> <table class="table table-striped table-bordered table-hover" id="mytable">
<thead>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品分类</th>
<th>商品价格(元)</th>
<th>商品产地</th>
<th>商品数量(件)</th>
</tr>
</thead>
<tbody>
<%--<tr>--%>
<%--<td>${goods.goodsId}</td>--%>
<%--<td>${goods.goodsName}</td>--%>
<%--<td>${goods.goodsType}</td>--%>
<%--<td>${goods.goodsPrice}</td>--%>
<%--<td>${goods.goodsFrom}</td>--%>
<%--<td>${goods.goodsAmount}</td>--%>
<%--</tr>--%>
</tbody>
</table>
</div>

ajax json struts JSP传递消息到action返回数据到JSP的更多相关文章

  1. 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)

    一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...

  2. asp.net mvc 使用Ajax调用Action 返回数据【转】

      使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=& ...

  3. 通过ajax从jsp页面传输数据到web层,并从web层返回数据给jsp页面

    jsp中ajax代码: 1 $.ajax({ var id = $("#studentid").val();//获取标签中的学生id url:'${pageContext.requ ...

  4. ajax get和post请求 后端接收并返回数据

    get请求$(function(){ //alert("23"); var x = "#page"; var y = "${ctx!}/static/ ...

  5. Ajax的GET,POST方法传输数据和接收返回数据

    //首先创建一个Ajax对象 function ajaxFunction(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new ...

  6. JQueryPagination分页插件,ajax从struts请求数据

    2017-07-16 学完了struts,做了个关于分页的小例子,用到了JQuery分页插件Pagination,先贴下插件下载地址 http://www.jq22.com/jquery-info13 ...

  7. jsp想js,action传值

    1.struts2 action如何向JSP的JS函数传值 action中定义变量 public class TestAction extends ActionSupport implements S ...

  8. springboot返回html和jsp

    一.返回html (1)添加maven依赖 <dependency>    <groupId>org.springframework.boot</groupId>  ...

  9. Struts+Spring+Hibernate项目整合AJAX+JSON

    1.什么是AJAX AJAX是 "Asynchronous JavaScript and XML" 的简称,即异步的JavaScript和XML. 所谓异步,就是提交一个请求不必等 ...

随机推荐

  1. [BZOJ3339]Rmq Problem / mex

    Description: 有一个长度为n的数组{a1,a2,-,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Hint: \(n \le 2*10^5\) Solution: 主席树好 ...

  2. (转)为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调用

    我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在“生产者-消费者”模型中,生产者线程发现缓冲区满了就等待,消费者线程通过 ...

  3. egret键盘事件监听

    document.addEventListener("keydown", function (event: any) { //alert(event.key); //console ...

  4. 浅谈AJAX基本实现流程

    1.js中ajax实现流程: (1) 创建XMLHttpRequest对象,也就是创建一个异步调用对象. (2) 创建一个新的HTTP请求,并指定该HTTP请求的方法.URL及验证信息. (3) 设置 ...

  5. 保存一个经常用的Makefile

    ############################################################# # Generic Makefile for C/C++ Program # ...

  6. JavaScript中Object值合并方法

    原文:https://www.cnblogs.com/fullstack-yang/p/8085206.html ------------------------------------ 前言:在日常 ...

  7. Docker配置参考

    Docker配置参考 一.参数列表 参考网址:https://docs.docker.com/engine/reference/commandline/dockerd/#options Usage: ...

  8. 阿里的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征: 速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser ...

  9. mysql数据库全备和全备还原(使用Xtrabackup)

    一.使用innobackupex创建全备 语法 innobackupex --user=DBUSER --password=DBUSERPASS /path/to/backup/dir/ innoba ...

  10. 每天一个linux命令(16):which

    1.命令简介 which (which) 命令的作用是在PATH变量指定的路径中搜索某个系统命令的位置并且返回第一个搜索结果.也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪 ...