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. 将map中的值赋值给一个java对象

    Map tag=new HashMap(); tag.put("001"," 张三"); tag.put("002","李四&qu ...

  2. json串转化成xml文件、xml文件转换成json串

    1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...

  3. Ruby用百度搜索爬虫

    Ruby用百度搜索爬虫 博主ruby学得断断续续,打算写一个有点用的小程序娱乐一下,打算用ruby通过百度通道爬取网络信息. 第三方库准备 mechanize:比较方便地处理网络请求,类似于Pytho ...

  4. Helm: Error: no available release name found

    如题,helm报这个错误 Helm: Error: no available release name found 错误的原因大概是因为 tiller没有正确的角色权限. 执行以下命令可解决这个问题. ...

  5. pygame-KidsCanCode系列jumpy-part16-enemy敌人

    接上回继续,这次我们要给游戏加点难度,增加几个随机出现的敌人,玩家碰到敌人后Game Over. 最终效果如下,头上顶个"电风扇"的家伙,就是敌人. 一.先定义敌人类 # 敌人类 ...

  6. [原创]AndroBugs_Framework Android漏洞扫描器介绍

    [原创]AndroBugs_Framework Android漏洞扫描器介绍 1 AndroBugs_Framework Android 漏洞扫描器简介 一款高效的Android漏洞扫描器,可以帮助开 ...

  7. 08、共享变量(Broadcast Variable和Accumulator)

    共享变量工作原理 Spark一个非常重要的特性就是共享变量.   默认情况下,如果在一个算子的函数中使用到了某个外部的变量,那么这个变量的值会被拷贝到每个task中.此时每个task只能操作自己的那份 ...

  8. docker dcm4chee

    The received images should show up in the UI of the Archive at http://localhost:8080/dcm4chee-arc/ui ...

  9. 导入的eclipse 中 maven项目,项目中已经加入lombok.jar包,但是不能编译成功

    原文地址:http://bbs.csdn.net/topics/390854507/ 答案是最后一个:单击工程名  Maven->Update Project

  10. 【推荐】Hutool 的通用工具类库

    摘自3.1.1版本作者发布原话,当时看到有点说不上的情绪,为作者的坚持.热爱点个赞. 已经想不起来是怎样结识 Hutool 的,但 Hutool 伴随几个项目的推进,获得了同事一致好评. 没经过实践和 ...