STRUTS2与JSON的LIST和MAP对象返回

<%@ 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 'JsonLogin.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" href="conf/themes/default/easyui.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="conf/themes/icon.css" type="text/css" media="screen" />
    <script src="conf/js/jquery-1.3.2.min.js"></script>
    <script src="conf/js/jquery.easyui.min.js"></script>
<script src="conf/js/easyui-lang-zh_CN.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript">

$(document).ready(function() {
    // 直接把onclick事件写在了JS中
    $("#submit").click(function() {
       $.ajax({       
           url : "jsonLogin.action",// 后台处理程序         
           type : "post", // 数据发送方式          
           dataType : "json",// 接收数据格式
           //dataType : "text",          
           data : "loginBean.userName="+$("#username").val() + "&loginBean.passwd="+$("#password").val(),// 要传递的数据          
           // 回传函数
            timeout:20000,// 设置请求超时时间(毫秒)。
            error: function () {// 请求失败时调用函数。
                  $("#msg").html("请求失败!"); 
            }, 
            /* success:function(data){ //请求成功后回调函数。
             var dataObj=eval("("+data+")");//如果dataType是text则需要转换为json对象
             $("#msg").html(dataObj.message);
             alert("用户名:"+dataObj.user.username+",密码:"+dataObj.user.password);
            }*/
           
            success:function(data){ // 请求成功后回调函数。如果dataType是json不就需要再转换为json对象
           
            //var resuletData= eval('('+data+')');
            //alert(resuletData.message); 
           
            //var resData = eval('(' + data + ")");
            //location.href='Temp.jsp';
$("#msg").html(data.message); 

var Reslist = data.list;
var listTemp="";
$.each(Reslist,function(i,tempLoginBean)
{  
            listTemp+=tempLoginBean.userName +"  ";
            //Reslist[i].userName也可以写成loginBean.userName
            });
$("#list").html("循环list===="+listTemp);

//循环取map中的值
var mapTemp = "";
var resMap =  data.dataMap;
alert("resMap=="+resMap['001'][0].userName);

$.each(resMap['002'],function(j,tempList)
{  
mapTemp += tempList.userName;
            });

$("#map").html("循环Map===="+mapTemp);


//手工去list的值
//$("#list").html("List===="+data.list[0].userName);

//手工去map的值
//$("#map").html("Map===="+data.dataMap[1][0].userName);

alert("用户名:"+data.loginBean.userName+",密码:" + data.loginBean.passwd);
           }
       });
    });
});

</script>



  </head>
 
  <body>
  <div id="msg"></div>
<div id="list"></div>
<div id="map"></div>
username:
<input name="loginBean.userName" id="username" type="text" />
<br />
password:
<input name="loginBean.passwd" id="password" type="password" />
<br />
<input type="submit" id="submit" value="登陆">
</body>
</html>
=====================================================
public class JsonLogin extends BSSBaseAction
{

    /**
     * 注释内容
     */
    private static final long serialVersionUID = 1L;
   
    private String message;
   
    private LoginBean loginBean;
   
    List<LoginBean> list;
   
    Map<String,List<LoginBean>> dataMap;
get set.....
public String loginJsonCheck() throws Exception
    {
        if ("admin".equals(loginBean.getUserName())
            && "admin".equals(loginBean.getPasswd()))
        {
//            message = "登陆成功!";
            System.out.println("user:" + loginBean.getUserName() + "   pass:"
                + loginBean.getPasswd());
            list    = LoginService.getAllUsers().get("001");
            dataMap = LoginService.getAllUsers();
            StringBuffer sbuff =  super.getJsonString("root",dataMap, null);
            message = sbuff.toString();
            System.out.println(message);
        }
        else
        {
            message = "用户名/密码有误!";
        }

        return SUCCESS;
    }  
===============================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<!-- 项目经历 -->
<package name="login" namespace="/" extends="json-default">
<action name="ideLogin" method="login" class="loginAction">
<result type="json">
<param name="ignoreHierarchy">false</param>
</result>
</action>
<action name="init" method="init" class="loginAction">
<result type="json"></result>
</action>

<!-- jsonLogin.aciton的配置文件 -->
<action name="jsonLogin" class="jsonLoginActoin" method="loginJsonCheck">
<result type="json">
<param name="includeProperties">
list.*,loginBean.*,message,dataMap.*</param>
</result> 
</action>



</package>
</struts>

你可能感兴趣的:(struts)