Struts2学习系列 -- Struts2增加json插件2 + ajax请求
2013-01-31 18:26
691 查看
主要jar包:
springframework 3.1.1版本
struts2 2.3.4.1版本
其他
js包:
jquery.ms.js
Domain
Device2.java:
public class Device2 {
Action类:
@Component
struct.xml重点配置如下
包必须继承json-default。
result的type为json,其中root参数的值result表示将对应action中属性为result属性值返回给客户端。
jsp页面:
js代码如下:
web.XML
spring_business.xml
springframework 3.1.1版本
struts2 2.3.4.1版本
其他
js包:
jquery.ms.js
Domain
Device2.java:
public class Device2 {
private String deviceId; private String userName; private String pwd; private String netAddr; private String netPort; // set/get方法略 }
Action类:
@Component
@Scope("prototype") @SuppressWarnings("serial") public class MyAction extends ActionSupport{ private Device2 device; private Integer id; private String result; public String add3(){ System.out.println("add3"); System.out.println(device); // result = "{name:pxj, password:'登录成功!'}"; result = device.toString(); return SUCCESS; } // set/get方法略 }
struct.xml重点配置如下
<package name="struts3" extends="struts-default,json-default" namespace="/"> <action name="myAction3" class="myAction" method="add3"> <result type="json"> <param name="root">result</param> </result> </action> </package>
包必须继承json-default。
result的type为json,其中root参数的值result表示将对应action中属性为result属性值返回给客户端。
jsp页面:
<form action="myAction3" method="post"> 设备id:<input type="text" name="deviceId3" id="deviceId3" /><br> 登陆账号:<input type="text" name="userName3" id="userName3" /> <br> 登陆密码:<input type="text" name="pwd3" id="pwd3" /> <br> <input type="button" onclick="onclickSave()" value="提交" /> <div id="info"></div> </form>
js代码如下:
<script src="jquery/jquery.min.js" type="text/javascript"></script> <script> function onclickSave() { $.post('myAction3', { "device.deviceId":$('#deviceId3')[0].value, "device.userName":$('#userName3')[0].value, "device.pwd":$('#pwd3')[0].value // loginName:$('#userName2')[0].value, // loginPwd:$('#pwd2')[0].value }, function(data){ alert(data);//请求返回的内容 },'json'); } </script>
web.XML
<context-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-INF/config/spring_business.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
spring_business.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- --> <context:annotation-config /> <context:component-scan base-package=" com.hry.action " /> </beans>
相关文章推荐
- Struts2学习系列 -- Struts2增加json插件
- Struts2-学习笔记系列(15)-ajax支持和JSON
- struts2学习笔记--使用struts2插件实现ajax处理(返回json数据)
- Struts2系列之 (1)通过JSON插件返回ajax数据
- Struts2-day03 文件上传, json格式 , 处理ajax请求 , 注解开发
- struts2之使用JSON插件实现Ajax(使用JSON插件)
- struts2的验证码及利用jquery发送ajax请求并利用json做数据交换
- ajax学习系列之七: 在请求和响应中使用 XML (1)
- Struts2:Json插件_Ajax
- AJAX学习之序列二:struts2 jquery json 实现AJAX 注册ID验证
- 【SSH三大框架】Struts2基础第八篇:Struts2用AJAX实现JSON插件的使用
- Web框架——小编浅谈Struts2中使用JSON插件实现Ajax
- struts2使用JSON插件开发Ajax
- 【struts2】【4】通过ajax向struts2 action请求json
- ajax请求插件vue-resource的学习
- JSP学习笔记(十四):关于ajax请求成功时返回的是jsonSring还是jsonObject的问题
- jquery发送ajax请求,以及struts2的action响应请求并传回json数据的做法
- 框架学习之struts2-05文件上传、下载、ajax整合json
- 【JAVAWEB学习笔记】28_jquery加强:json数据结构、jquery的ajax操作和表单校验插件
- struts2之使用JSON插件实现Ajax(在struts.xml中处理要序列化的属性)