struts2.1 json

本文介绍如何在Struts2框架中使用JSONPlugin插件处理JSON数据交互。通过示例展示了如何配置Struts2,创建处理JSON数据的动作类,并在前端使用jQuery发起GET请求获取JSON数据。

首先去下载 http://code.google.com/p/jsonplugin/downloads/list 先

还有记得加埋strtus官方lib里面有关json的jar包

然后添加测试action

Java代码
  1. package  com.json;  
  2.   
  3. import  java.util.HashMap;  
  4. import  java.util.Map;  
  5. import  javax.servlet.http.HttpServletRequest;  
  6. import  org.apache.struts2.interceptor.ServletRequestAware;  
  7. import  com.googlecode.jsonplugin.annotations.JSON;  
  8. import  com.opensymphony.xwork2.ActionSupport;  
  9.   
  10. @SuppressWarnings ( "unchecked" )  
  11. public   class  JSONExample  extends  ActionSupport  implements   ServletRequestAware{  
  12.   
  13.     private   static   final   long  serialVersionUID = 8465922425903754099L;  
  14.     private  String tip;  
  15.     private  Map map =  new  HashMap();  
  16.     private  HttpServletRequest request;  
  17.   
  18.     @JSON (name= "testMap" )  
  19.     public  Map getMap() {  
  20.         return  map;  
  21.     }  
  22.   
  23.     public   void  setMap(Map map) {  
  24.         this .map = map;  
  25.     }  
  26.   
  27.     @JSON (name= "newName" )  
  28.     public  String getTip() {  
  29.         return  tip;  
  30.     }  
  31.   
  32.     public   void  setTip(String tip) {  
  33.         this .tip = tip;  
  34.     }  
  35.   
  36.       
  37.     @Override   
  38.     public  String execute()  throws  Exception {  
  39.         setTip( "系统中已有用户名,请重新选择一个!" );  
  40.         this .map.put( "ok""dd" );  
  41.         this .map.put( "ss""lal" );  
  42.         System.out.println("************" + this .request.getParameter( "name" ));  
  43.         return  SUCCESS;  
  44.     }  
  45.   
  46.     @Override   
  47.     public   void  setServletRequest(HttpServletRequest arg0) {  
  48.         this .request = arg0;  
  49.     }  
  50.   
  51.   
  52. }  
package com.json;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public class JSONExample extends ActionSupport implements  ServletRequestAware{

	private static final long serialVersionUID = 8465922425903754099L;
	private String tip;
	private Map map = new HashMap();
	private HttpServletRequest request;

	@JSON(name="testMap")
	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	@JSON(name="newName")
	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	
	@Override
	public String execute() throws Exception {
		setTip( "系统中已有用户名,请重新选择一个!");
		this.map.put("ok", "dd");
		this.map.put("ss", "lal");
		System.out.println("************"+this.request.getParameter("name"));
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}


}

 页面使用jqurey

Java代码
  1. <script type= "text/javascript"  src= "jquery.js" ></script>  
  2. <script type="text/javascript" >  
  3.     $(document).ready( function() {  
  4.         $("a" ).click( function() {  
  5.             $.getJSON("JSONExample.action" , "name=" +$( "#asdf" ).value, function(json){  
  6.               $("#ss" ).html(json.testMap.ss);  
  7.               $("#ok" ).html(json.testMap.ok);  
  8.               $("#newName" ).html(json.newName);  
  9.             });   
  10.         });  
  11.     });  
  12. </script>  
  13. <a href="#" >dfdf</a>  
  14. <div id ="ss" ></div><br>  
  15. <div id="newName" ></div><br>  
  16. <div id ="ok" ></div>  
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
	$(document).ready( function() {
		$("a").click( function() {
			$.getJSON("JSONExample.action","name="+$("#asdf").value, function(json){
			  $("#ss").html(json.testMap.ss);
			  $("#ok").html(json.testMap.ok);
			  $("#newName").html(json.newName);
			}); 
		});
	});
</script>
<a href="#">dfdf</a>
<div id ="ss"></div><br>
<div id="newName"></div><br>
<div id ="ok"></div>

 struts.xml中加入

Java代码
  1. <constant name= "struts.i18n.encoding"  value= "UTF-8" />  
  2. <package  name= "com.json"   extends = "json-default" >  
  3.         <action name="JSONExample"   class = "com.json.JSONExample" >  
  4.             <result type="json"  />  
  5.         </action>  
  6.     </package >  
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="com.json" extends="json-default">
		<action name="JSONExample" class="com.json.JSONExample">
			<result type="json" />
		</action>
	</package>

 然后试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值