Struts2 json plugin实战3 - JSON RPC
2010-07-29 08:53
246 查看
1. 首先是在struts2的工程里添加struts2-json-plugin.xxx.jar库,如果是使用的maven,添加以下dependency
2. 创建Action类和一个用到的User类
Action类内容
User类内容
3. Spring配置文件
4. Struts2配置文件
5. 新建一个jsp文件json.jsp用来做测试
6. 启动web容器测试,可以使用Tomcat或者maven自带的jetty,然后访问
http://localhost:8080/json/json.jsp
此时可以点击页面上的链接来测试结果。
参考:https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.1.8.1</version> </dependency>
2. 创建Action类和一个用到的User类
Action类内容
import com.opensymphony.xwork2.Action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.json.annotations.SMDMethod; public class JSONExample3 { private List<User> userList = new ArrayList<User>(); public String execute() { return Action.SUCCESS; } @SMDMethod public List<User> getMyUserList(int size) { for (int i = 0; i < size; i++) { User user = new User("id_" + i, "username_" + i, "password_" + i, "desc_" + i); userList.add(user); } return userList; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } }
User类内容
import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private String id; private String username; private String password; private String description; public User() { } public User(String id, String username, String password, String description) { this.id = id; this.username = username; this.password = password; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
3. Spring配置文件
<?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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd" default-lazy-init="true"> <bean id="jsonExample3" class="JSONExample3" scope="prototype"/> </beans>
4. Struts2配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml" /> <package name="json" namespace="/json" extends="struts-default" > <result-types> <result-type name="json" class="org.apache.struts2.json.JSONResult"/> </result-types> <interceptors> <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/> </interceptors> <action name="smdAction" class="jsonExample3"> <interceptor-ref name="json"> <param name="enableSMD">true</param> </interceptor-ref> <result type="json"> <param name="enableSMD">true</param> </result> </action> </package> </struts>
5. 新建一个jsp文件json.jsp用来做测试
<html> <head> <title></title> <mce:script type="text/javascript" src="http://www.google.com/jsapi?key=helloworld" mce_src="http://www.google.com/jsapi?key=helloworld"></mce:script> <mce:script type="text/javascript"><!-- google.load("jquery", "1.4.1"); google.load("dojo", "1.1"); function testSmdAction() { //create service object(proxy) using SMD (generated by the json result) var service = new dojo.rpc.JsonService("/json/smdAction.action"); //execute remote method var defered = service.getMyUserList(10); //attach callback to defered object defered.addCallback(function(data) { var s = ""; for (var idx = 0; idx < data.length; idx++) { s += "user["+idx+"].id=" + data[idx].id + "/n"; } alert(s); }); } function init() { dojo.require("dojo.io.script"); dojo.require("dojo.rpc.JsonService"); } // --></mce:script> </head> <body style="margin: 20px;" mce_style="margin: 20px;" onload="init();"> <h3>json test</h3> <ul> <li><a href="#" mce_href="#" onclick="testSmdAction()">SMD Action</a></li> </ul> </body> </html>
6. 启动web容器测试,可以使用Tomcat或者maven自带的jetty,然后访问
http://localhost:8080/json/json.jsp
此时可以点击页面上的链接来测试结果。
参考:https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin
相关文章推荐
- Struts2 json plugin实战3 - JSON RPC
- Struts2 json plugin实战1
- Struts2 json plugin实战2
- Struts2 json plugin实战2
- Struts2 json plugin实战1
- 使用struts2 的jsonplugin插件与opensessioninview和hibernate延迟加载时遇到的问题
- 关于struts2.3.4.1 struts2-json-plugin的问题
- struts2 使用jsonplugin
- jsonplugin-0.33.jar与struts2-json-plugin-2.1.8.1.jar冲突问题
- Struts2-Json-Plugin 的使用(翻译自官方文档)
- Struts2整合Json插件时的一个隐蔽BUG-----struts2-json-plugin-2.1.8
- [struts2]2.3.14 json-plugin 存在bug<java.lang.NoSuchFieldException: DEFAULT_PARAM>
- 【SSH网上商城项目实战07】Struts2和Json的整合
- 使用 Struts2 JSON plugin ( Struts2 + jQuery )
- struts2-json-plugin使用
- 关于使用struts2-json-plugin 插件,Json返回List对象数据为空解决方案
- 记录struts2 json plugin 对字符串数组类型的处理
- [struts2]2.3.14 json-plugin 存在bug<java.lang.NoSuchFieldException: DEFAULT_PARAM>
- Extjs整合struts2的jsonplugin的方法和注意事项
- struts2-json-plugin 使用