ajax json struts JSP传递消息到action返回数据到JSP
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的更多相关文章
- 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)
一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...
- asp.net mvc 使用Ajax调用Action 返回数据【转】
使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=& ...
- 通过ajax从jsp页面传输数据到web层,并从web层返回数据给jsp页面
jsp中ajax代码: 1 $.ajax({ var id = $("#studentid").val();//获取标签中的学生id url:'${pageContext.requ ...
- ajax get和post请求 后端接收并返回数据
get请求$(function(){ //alert("23"); var x = "#page"; var y = "${ctx!}/static/ ...
- Ajax的GET,POST方法传输数据和接收返回数据
//首先创建一个Ajax对象 function ajaxFunction(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new ...
- JQueryPagination分页插件,ajax从struts请求数据
2017-07-16 学完了struts,做了个关于分页的小例子,用到了JQuery分页插件Pagination,先贴下插件下载地址 http://www.jq22.com/jquery-info13 ...
- jsp想js,action传值
1.struts2 action如何向JSP的JS函数传值 action中定义变量 public class TestAction extends ActionSupport implements S ...
- springboot返回html和jsp
一.返回html (1)添加maven依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- Struts+Spring+Hibernate项目整合AJAX+JSON
1.什么是AJAX AJAX是 "Asynchronous JavaScript and XML" 的简称,即异步的JavaScript和XML. 所谓异步,就是提交一个请求不必等 ...
随机推荐
- BZOJ2240 : ural1676 Mortal Combat
首先如果最大匹配不足$n$个那么显然每条边都不可能在匹配为$n$的方案中. 对于一条边$(u,v)$,如果它可能在最大匹配中,有两种情况: $1.(u,v)$是当前方案的匹配边. $2.$可以沿着$( ...
- Making the Grade [POJ3666] [DP]
题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,代价为Σabs(i变化后-i变化前),序列长度<=2000,单个数字<=1e9 输入:(第一行表示序列长度,之后一行一个表示 ...
- Django——User-Profile
Profile作用:User内置的字段不够完善,导致创建的用户信息单一,Profile就是为了对User进行扩展,即丰富用户信息 在models中创建Profile类,添加字段user与User形成O ...
- 【Luogu3381】【模板】缩点
本文同步发表于https://www.zybuluo.com/Gary-Ying/note/1235385 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最 ...
- HTTP协议内容1
一.请求协议(浏览器---àserver) 格式: (1) 请求首行 ://请求方式 请求路径 协议和版本 例如:GET /index.html HTTP/1.1 (2) 请求头信息:// ...
- CSS_常见布局
1.一列布局——常用于网站首页. html: <div class="top"></div> <div class="main"& ...
- 本地文件上传GitHub
(1)mkdir 项目名称(2)cd 项目名称(3)git init 把它变成可管理的Git仓库(4)git status 查看状态(5)git add . 点用空格隔开(6)git status ...
- poj3616 Milking Time(状态转移方程,类似LIS)
https://vjudge.net/problem/POJ-3616 猛刷简单dp的第一天第二题. 这道题乍一看跟背包很像,不同的在于它是一个区间,背包是定点,试了很久想往背包上套,都没成功. 这题 ...
- Java的内存管理机制之内存区域划分
各位,好久不见.先做个预告,由于最近主要在做Java服务端开发,最近一段时间会更新Java服务端开发相关的一些知识,包括但不限于一些读书笔记.框架的学习笔记.和最近一段时间的思考和沉淀.先从Java虚 ...
- java利用Tesseract 识别身份证号码
安装Tesseract http://blog.csdn.net/hiredme/article/details/50894814 http://blog.csdn.net/yoara/article ...