关于Struts传递json给easyui的随笔
今天在公司写测试代码,由于公司用的是ssh框架做的商城项目,我想先实现下简单的增删改查,奈何没有很好的后台页面(毕竟不能测试代码直接在他的项目里改啊)
所以想到了淘淘商城中有这个后台的管理页面,打算一试,奈何struts没学好,琢磨好几小时如何把json数据回传给easyui的页面,这里还是推荐大家用谷歌
后台页面使用的是easyui,直接放其中Customer添加页面和后台action的代码吧
customer-add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<link
href="${fileUrlConfig.fileServiceUploadRoot}/zcg/js/kindeditor-4.1.10/themes/default/default.css"
type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8"
src="${fileUrlConfig.fileServiceUploadRoot}/zcg/js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
<script type="text/javascript" charset="utf-8"
src="${fileUrlConfig.fileServiceUploadRoot}/zcg/js/kindeditor-4.1.10/lang/zh_CN.js"></script>
<div style="padding:10px 10px 10px 10px">
<!-- 这是个需要提交的表单,可以用来增加商品 -->
<form id="itemAddForm" class="itemForm" method="post">
<table cellpadding="5">
<!-- <tr>
<td>商品类目:</td>
<td>
<a href="javascript:void(0)" class="easyui-linkbutton selectItemCat">选择类目</a>
<input type="hidden" name="cid" style="width: 280px;"></input>
</td>
</tr> -->
<tr>
<td>登录用户名:</td>
<td><input class="easyui-textbox" type="text" name="loginName"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input class="easyui-textbox" type="password"
name="password" data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>昵称:</td>
<td><input class="easyui-textbox" type="text" name="nickName"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>手机号:</td>
<td><input class="easyui-textbox" type="text" name="phone"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input class="easyui-textbox" type="text" name="email"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>头像地址:</td>
<td><input class="easyui-textbox" type="text" name="photoUrl"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
<!-- <tr>
<td>上传头像:</td>
<td><a href="javascript:void(0)"
class="easyui-linkbutton picFileUpload">上传图片</a> <input
type="hidden" name="image" /></td>
</tr> -->
<tr>
<td style="text-align:left;">会员类型:</td>
<td style="text-align:left">
<span class="radioSpan">
<input type="radio" name="type" value="1">采购商</input>
<input type="radio" name="type" value="2">供应商</input>
</span>
</td>
</tr>
<tr>
<td style="text-align:left;">分享类型:</td>
<td style="text-align:left">
<span class="radioSpan">
<input type="radio" name="shareType" value="1">普通会员</input>
<input type="radio" name="shareType" value="2">超级终端</input>
<input type="radio" name="shareType" value="3">个人分销商</input>
<input type="radio" name="shareType" value="4">县市分销商</input>
<input type="radio" name="shareType" value="5">省级分销商</input>
<input type="radio" name="shareType" value="6">操盘手(店铺代运营)</input> </span>
</td>
</tr>
<tr>
<td style="text-align:left;">商品交易状态:</td>
<td style="text-align:left">
<span class="radioSpan">
<input type="radio" name="isCanBuy" value="1">可以采购</input>
<input type="radio" name="isCanBuy" value="2">不采购</input>
</span>
</td>
</tr>
<tr>
<td>支付密码:</td>
<td><input class="easyui-textbox" type="password"
name="payPassword" data-options="required:true" style="width: 280px;"></input></td>
</tr>
<tr>
<td>微信公众号:</td>
<td><input class="easyui-textbox" type="text" name="openid"
data-options="required:true" style="width: 280px;"></input></td>
</tr>
</table>
<input type="hidden" name="itemParams" />
</form>
<div style="padding:5px">
<a href="javascript:void(0)" class="easyui-linkbutton"
onclick="submitForm()">提交</a> <a href="javascript:void(0)"
class="easyui-linkbutton" onclick="clearForm()">重置</a> </div>
</div>
<script type="text/javascript">
//提交表单
function submitForm() {
debugger;
//有效性验证
if (!$('#itemAddForm').form('validate')) {
$.messager.alert('提示', '表单还未填写完成!');
return;
}
$.post("/zcg/customersave.action", $("#itemAddForm").serialize(), function(data) {
if (data.status == 200) {
$.messager.alert('提示', '新增用户成功!');
}
});
} function clearForm() {
$('#itemAddForm').form('reset');
itemAddEditor.html('');
}
</script>
如上述代码可见,我这里使用了ajax提交了表单之后等待后台回传个带有状态码的json数据,这样就能调用easyui页面中的ajax响应弹窗,从而完成交互
但是在网上查了好久都没找到几个靠谱的方法,要么讲的太乱……
action的代码
package shop.hellxz.action; import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map; import javax.json.Json; import org.apache.struts2.json.JSONUtil;
import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import cn.emay.slf4j.helpers.Util;
import shop.zhangchenguang.pojo.Customer2;
import shop.zhangchenguang.service.CustomerService;
import util.action.BaseAction;
import util.other.Utils; /**
* 测试代码Action
* @author Hellxz
*
*/
public class CustomerAction extends BaseAction{ //注入service层
private CustomerService customerServiceImpl;
//需要写get、set方法
public CustomerService getCustomerServiceImpl() {
return customerServiceImpl;
}
public void setCustomerServiceImpl(CustomerService customerServiceImpl) {
this.customerServiceImpl = customerServiceImpl;
}
//跳转到主页
public String index() throws IOException{
return "success";
}
//保存用户信息
public void customerSave() throws IOException{
//获取输出流
response.setContentType("json/application;charset=UTF-8");
PrintWriter out = response.getWriter();
//获取传入数据对象
Customer2 customer = new Customer2();
customer.setLoginName(request.getParameter("loginName"));
customer.setPassword(request.getParameter("password"));
customer.setNickName(request.getParameter("nickName"));
customer.setPhone(request.getParameter("phone"));
customer.setEmail(request.getParameter("email"));
customer.setPhotoUrl(request.getParameter("photoUrl"));
customer.setType(Integer.parseInt(request.getParameter("type")));
customer.setShareType(Integer.parseInt(request.getParameter("shareType")));
customer.setIsCanBuy(Integer.parseInt(request.getParameter("isCanBuy")));
customer.setPayPassword(request.getParameter("payPassword"));
customer.setOpenId(request.getParameter("openid"));
//补全数据……还没写
//调用service保存对象
Object obj = customerServiceImpl.saveOrUpdateObject(customer);
/**
* 如果保存的对象是非空的,那么已经保存成功
* 如果为空,则保存失败
* 无论保存成功那个与否,都要把json对象写回给客户端,进行判断
*/
if(Utils.objectIsNotEmpty(obj)){
out.println(JSON.toJSONString(checkOK()));
}else{
out.println(JSON.toJSONString(checkFail()));
}
}
/**
* @return json对象状态200
*/
@SuppressWarnings({"unchecked","rawtypes"})
public Object checkOK(){
Map m = new HashMap<>();
m.put("status", 200);
m.put("msg", "添加成功");
m.put("data", null);
return m;
}
/**
* @return json对象状态500
*/
@SuppressWarnings({"unchecked","rawtypes"})
public Object checkFail(){
Map m = new HashMap<>();
m.put("status", 500);
m.put("msg", "添加失败");
m.put("data", null);
return m;
} }
附上struts的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="zcg" namespace="/zcg" extends="basePackage">
<action name="index" class="shop.hellxz.action.CustomerAction" method="index">
<interceptor-ref name="baseStack"/>
<result name="success">/WEB-INF/hellxz/jsp/index.jsp</result>
</action>
<action name="*-*" class="shop.hellxz.action.SendAction">
<result name="send">/WEB-INF/hellxz/jsp/{1}-{2}.jsp</result>
</action>
<action name="customersave" class="shop.hellxz.action.CustomerAction" method="customerSave"> </action>
</package>
</struts>
到这里大家可能明白是怎么实现的了,老规矩捋一下流程:
1、在struts配置文件中让表单提交的请求响应到action中,不需要定义result,
2、响应进来之后,我们定义的方法需要设置void返回值,在方法体就可以通过老办法 request.getParameter()方法获取这些表单中的参数,然后放到pojo中持久化
3、持久化成功会返回一个新的对象,判断这个对象是否为空,然后通过这个结果来对应应该用到的检查方法,这里使用了alibaba的Fastjson中的JSON对象的toJsonString()方法来实现把对象转换成json串
4、通过response.getWriter()方法拿到的输出流,我们使用println()方法打印出来那个json串给浏览器端
5、浏览器端的ajax收到带有状态码200的json串,弹窗提示用户存储成功
说着简单,其实就是jsp和servlet时候常用的方法,springmvc用习惯了,反而忘了最基本的方法,罪过罪过。
关于Struts传递json给easyui的随笔的更多相关文章
- 用easyui从servlet传递json数据到前端页面的两种方法
用easyui从servlet传递json数据到前端页面的两种方法 两种方法获取的数据在servlet层传递的方法相同,下面为Servlet中代码,以查询表中所有信息为例. //重写doGet方法 p ...
- Struts的JSON机制
需要加入jar包 Struts的JSON帮助我们自动将对象解析为JSON对象,不用我门借助第三方进行JSON的解析 .具体的使用机制如下: 1.Action类 package StrutsJSON; ...
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
先看一下我要实现的功能界面: 这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面 ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- jQuery调用WCF服务传递JSON对象
下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...
- Jquery调用Webservice传递Json数组
Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...
- WebApi传递JSON参数
开发过程中经常进行JSON的传递,在WebApi中传递JSON字串时,会发现服务器端接收到不参数值,看下面代码 服务端: public void Post([FromBody]string value ...
- 从Python传递JSON到JavaScript
OS: Windows 8.1 with update 关键字:Python 3.4,HTML5,JSON,JavaScript 1.LocalServer.py,启动server,打开网页,传递JS ...
- Jquery Ajax方法传递json到action
ajax向后台传入json需要设置option,如下 contentType:'application/json' data:Json.Stringify(jsObj) 后台处理复杂json对象(不知 ...
随机推荐
- 字符串MD5加密运算
public static string GetMd5String(string str) { MD5 md5 = MD5.Create(); by ...
- Nodejs真.多线程处理
前言 Threads à gogo 是nodejs 的原生模块,使用这个模块可以让nodejs 具备多线程处理功能 安装方法 npm install threads_a_gogo 下载测试源码 git ...
- [E::hts_idx_push] NO_COOR reads not in a single block at the end 10 -1
在分析转录组数据时,用bowtie2比对生成的bam文件,下一步call peak使用m6Aviewer,需要bam文件的index文件.所以我直接敲命令 samtools index xx.bam ...
- 微信小程序开发模板消息的时候 出现 errcode: 41028, errmsg: "invalid form id hint:
小程序开发模板消息的时候 出现 errcode: 41028, errmsg: "invalid form id hint: 我是使用的微信支付发送模板消息,提示的formid无效的 大家 ...
- 微信小程序开发之选项卡
选项卡是web开发中经常使用到的一个模块,在小程序中竟然没有,这里参考别人的文章自己做了一个双选项卡 实现思路: 通过绑定swichNav事件来控制currentTab(当前选项卡)和isShow(是 ...
- ArcGis连接oracle失败:ORA-6413:连接未打开
问题: 通过ARCMap 添加Oracle数据库连接时提示,ORA-6413:连接未打开. 运行环境: ArcGis 10.2 Oracle 10g 解决方法: 通过上网查找解决方法,网友说" ...
- C盘里的桌面文件移到E盘里了,然后E盘里的文件都显示到桌面上了,怎么将桌面文件还原回C盘
1 . 直接按Windows键+R,打开"运行"对话框,在输入框中输入"regedit"命令,会打开注册表编辑窗口: 2.打开注册表文件将HKEY_CURREN ...
- [代码审计]青云客Cms前台有条件注入至getshell,后台xss至getshell、至弹你一脸计算器
之前写了一篇关于青云客cms的文章,发在了t00ls,就不copy过来了. 给个链接,好记录一下. https://www.t00ls.net/thread-43093-1-1.html
- Java框架之Spring(五)
本文主要介绍Spring中, 1 Spring JDBC 2 使用注解方式管理事务的传播行为 3 采用XML 方式配置事务 4 SH 整合 5 SSH 整合 一.Spring JDBC 1) 导包 , ...
- Jerry的CDS view自学系列
My CDS view self study tutorial - part 1 how to test odata service generated by CDS view https://blo ...