客戶端傳遞json與服務端異步交互問題(struts2+jquery)

   ajax異步傳遞json

  最快在做項目時,遇到了這樣的問題,瀏覽器通過ajax方式異步傳遞JSON交互問題,會出現各種問題。經過幾天的搜索,終於得到解決~

  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" "http://www.w3.org/TR/html4/loose.dtd">
<html>

  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajaxTest.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"/>
	
	<script type="text/javascript" src="scripts/jquery-1.9.js"></script>
	<script type="text/javascript">
      $(document).ready(function(){
          $(".submit").click(function(){
              $.ajax({
            	  url:"getJsonAction2",
            	  type:"POST",
            	  dataType:"json",
            	  contentType:"application/json;charset=utf-8",
            	  success:function(returnData,status)
            	  {
            		  person1= returnData[0];
            		  person2= returnData[1];
            	      alert(person1.username);
            	      alert(person2.username);
            	  }
              });              	  
         	  
          });   	  
      });	
	</script>
  </head>
  
  <body>
     
    <input type="submit" value="getJson" class="submit" >
   
  </body>
</html>

Action代碼:

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class GetAjaxAction extends ActionSupport
{
	@Override
	public String execute() throws Exception
	{
		Person person1 = new Person();
		
		person1.setUsername("zhangsan");
		person1.setPassword("123456");
		person1.setAge(16);
		person1.setAddress("beijing");
		
		Person person2 = new Person();
		
		person2.setUsername("lisi");
		person2.setPassword("123456");
		person2.setAddress("shanghai");
		person2.setAge(20);
		
		ArrayList<Person> list=  new ArrayList<Person>();
		
		list.add(person1);
		list.add(person2);
		
		Gson gson=  new Gson();
		
		String result = gson.toJson(list);
		
		System.out.println(result);
		
		HttpServletResponse response =ServletActionContext.getResponse();
		
		PrintWriter out= response.getWriter();
		
		out.println(result);
		
		out.flush();
		out.close();
		
		return null;
	}

}
struts.xml配置:

 <action name="getJsonAction2" class="com.fileupload.GetAjaxAction"></action>

結果頁面:

返回的JSON的對象數組,這樣就可以解決很多問題了~當然也可以從jsp傳送數據到服務端,比$.ajax更好用的方法還有$.post()和$.load(),但都基於ajax()方法實現的,而且ajax()方法還有很多可選的參數,有興趣的可以自己練習用下~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章