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. [Vijos1532]区间 (差分约束)

    又是一题我不会的模板题…… 讲一下差分约束吧 差分约束 参考博客 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统.— ...

  2. yii2场景的应用(scenarios)

    例如: 现在在 post表里面有 title image content 三个的字段,当我创建一个 post 的时候,我想三个字段全部是必填项,但是你修改的时候,title content 两个字段是 ...

  3. Java 接口 Closeable

    该接口位于java.io包下,声明如下:public interface Closeable extends AutoCloseable.关闭流并释放与该流关联的所有系统资源.如果已经关闭该流,则调用 ...

  4. strtok strchr strrchr strchrnul

    NAME       strchr, strrchr, strchrnul - locate character in string SYNOPSIS       #include <strin ...

  5. 学习Selenium的历程

    Selenium资源包下载 我这段时间在学习Web自动化测试方面的知识,在搭建相应的环境上出现了问题.去Selenium官网下载相对应得包,老是下载不了.而如果直接到CSDN等上下载,需要积分,或者下 ...

  6. Windows平台下SVN安装配置及使用

    原文链接:https://www.cnblogs.com/snake-hand/archive/2013/06/09/3130022.html,等有空了玩一玩吧,现在没空.

  7. mvc中使用Pagination,对其进行再封装

    对其进行再次封装: (function($) { $["fn"]["easyPaging"] = function(o) { if (!o.pageSelect ...

  8. lettcode21. Merge Two Sorted Lists

    lettcode21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ne ...

  9. leetcode笔记--水箱问题

    类型的引用:Solution *s=new Solution(); 1.Container With Most Water Given n non-negative integers a1, a2, ...

  10. gpg使用说明

    http://blog.163.com/ywz_306/blog/static/13257711201121921452480/