Json+Struts2+Jquery(自己总结的)

Struts2 签入了Json插件,只需引入包struts2-json-plugin-2.2.1.jar即可,但是很蛋疼的是包与包之间会冲突,不同的版本会冲突,我采用的是

经测试没有问题。Jquery所使用的库是jquery-2.0.2.js

struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="example"  extends="json-default">
<action name="test" class="ReturnJson">
<!-- 配置类型的json的Result -->
<result type="json">
<param name="root">map</param>
</result>
</action>
</package>
</struts>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

java文件:

ReturnJson.java:



import java.util.HashMap;
import java.util.Map;


import org.apache.struts2.json.annotations.JSON;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


public class ReturnJson extends ActionSupport {


private Map<String, Person> map;
@JSON
public Map<String, Person> getMap() {
return map;
}

public void setMap(Map<String, Person> map) {
this.map = map;
}

@Override
public String execute(){
System.out.println("进入action");
Person person1 = new Person();
person1.setId(1);
person1.setName("chenmeng");
person1.setOld(25);

Person person2 = new Person();
person2.setId(2);
person2.setName("jinjun");
person2.setOld(23);

map = new HashMap<String, Person>();
map.put("p1", person1);
map.put("p2", person2);
System.out.println("退出action");
return SUCCESS;
}

}

Person.java文件:

import java.io.Serializable;
public class Person implements Serializable {
private int id;
private String name;
private int old;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOld() {
return old;
}
public void setOld(int old) {
this.old = old;
}
}

Jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="jquery/jquery-2.0.2.js"></script>
<script type="text/javascript">
function submit() {
var url = "test.action";
$.get(url,null,callback,"json")
}
</script>


<script type="text/javascript">
function callback(data){
  var obj = eval(data);
for(var o in obj){
alert(obj[o].id + "--"+obj[o].name+ "--"+obj[o].old);
}
}
</script>
</head>
  
  <body>
    This is my JSP page. <br>
    <input type="button" value="sbmit" οnclick="submit()"/>
    
    <div id="show"></div>
  </body>
</html>


通过测试代码可正常运行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值