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. 所谓异步,就是提交一个请求不必等 ...
随机推荐
- BZOJ1290 : [Ctsc2009]序列变换
设$f[i][j]$表示$a[i]$改成$j$时的最小总代价. 若$a[i]<A(i-1)+1$,则不妨将其强行改成$A(i-1)+1$,如此处理之后$\min(f[n][1..Q])$就是答案 ...
- token和盐
// 盐,加密后密码获取 Map<String, String> map = new HashMap<String, String>(); map.put(&quo ...
- Flask对象配置
Flask的配置就是在 app.config 中添加一个键值对,但是你存进去的键必须是config中应该存在的,如果不再存在的话,它会默认无用,就这么放着 config中有用的key { 'DEBUG ...
- Java 读取 txt 文件内容到容器 List
方法一: 一.桌面上准备 DataObject.txt 文件,内容为: 二.打开 Eclipse,编写代码如下: import java.io.BufferedReader; import java. ...
- jQuery 学习(1)——认识jQuery
1.下载 下载地址:http://jquery.com/download/ jquery-3.2.1.js——用于开发和学习(229K) jquery-3.2.1.min.js——用于项目和产品(31 ...
- C# GDI+之Graphics类 z
GDI+是GDI的后继者,它是.NET Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘制各种图形图像,可以用于绘制各种数据图像.数学仿真等. Graphics类是GDI+的核心, ...
- ASP.NET微信公众号获取AccessToken
access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存.access_token的存储至少要保留512个字符空间.acces ...
- [转]通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题
转:http://blog.csdn.net/zhengwish/article/details/51602059 通过设置nginx的client_max_body_size解决nginx+php上 ...
- wsdl 生成 java 代码 java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例 推荐使用, 并且设置了 utf8
推荐使用, 并且设置了 utf8 wsdl2java -p cn.smborderservice -encoding utf-8 -d f:\logink\src -all -autoNameRes ...
- gitlab简单使用教程【转】
平时一直是用git来管理代码仓库,也用过一段时间github,但是github免费版不能建私有仓库.后来转到了bitbucket,后来被atlassian收购后有点不适应,而且在国内访问经常连不上.还 ...