日期:2014-05-16  浏览次数:20425 次

ajax+json+Struts2实现list传递

     由于实习需要,需要通过ajax来获取后台的List集合里面的值。由于前面没有接触过,所以今天就来研究下了。

     一、首先需要下载JSON依赖的jar包。它主要是依赖如下:

      json-lib-2.2.2-jdk15

      ezmorph-1.0.4
      commons-logging-1.0.4
      commons-lang-2.4
      commons-collections-3.2.1

      commons-beanutils


     二、实例。

     1、身份证错误信息Bean类(ErrorCondition.java)

/**
 *@Project: excel
 *@Author: chenssy
 *@Date: 2013-4-4
 *@Copyright: chenssy All rights reserved.
 */
public class ErrorCondition {
	private String name; // 姓名
	private String idCard; // 身份证
	private String status; // 错误状态
	private String message; // 错误信息

	ErrorCondition(String name,String idCard,String status,String message){
		this.name = name;
		this.idCard = idCard;
		this.status = status;
		this.message = message;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

     2、JSP页面(index.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>	
  	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.7.2.js"></script>
  </head>
  
  <body>
  		<input type="button" value="点我显示数据" id="clickMe">
    	<table id="showTable" border="1">
    		<tr>
    			<td>姓名</td>
    			<td>身份证</td>
    			<td>错误状态</td>
    			<td>错误信息</td>
    		</tr>
    	</table>
   <script>
  		$("#clickMe").click(function(){
  			var url = "json/jsonTest.action";
  			$.ajax({
  				type:'get',
  				url:url,
  				dataType: 'json',
  				success:function(data){
  					$.each(data,function(i,list){
  						var _tr = $("<tr><td>