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

ssh 框架 jquery ajax 向action中传入bean对象,并解析

1.请先看jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//System.out.print("path:"+path+"basePath:"+basePath);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加信息</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="<%=path%>/js/jquery/jquery-1.2.6.js"></script>  
	<script type="text/javascript" language="javascript">
		$(document).ready(function(){ 
    		$("#login").click(function(){  
        		login();  
        	});  
		}); 
		
		function Mybean(){
			this.title="你大爷威武";
			this.content="你大爷威武!!!";
			this.id="11";
		}
		 
		function login(){
		    //alert(11)
		    var mybean = new Mybean();
		    var url = "ajax_login2.action";  
		    var params = {
			    "user.username":$("#name").attr("value"),
			    "user.password":$("#password").attr("value"),
			    "haha":"你大爷的",
			    "result":JSON.stringify(mybean)
		    };  
		   
		    //alert("params:"+params);
		    
		    //alert(22);
		    
		    /*  
		    $.getJSON(url,params,function callback(data){  
		        // convert to json object 
		        
		        alert("data:"+data);
		         
		        var user = eval("("+data+")");//  
		        
		        alert("user:"+user);  
		          
		        $("#result").each(function(){  
		            $(this).html('welcome ,' + user.name);  
		            });  
		    });
		    */
		    $.ajax({
		    	type:"post",
		    	url:url,
		    	async:false,
		    	data:params,
		    	
		    	
		    	success:function(data){
		    		 alert(data);
		    		 
		    		 var user = eval("("+data+")");// 
		    		 alert(user)
		    		 //alert("命令为:"+user.command);
		    		 //alert("令为:"+user.list[0].id);
		    		 
		    		 //alert("用户名:"+user.users.username);
		    		 //alert("密码:"+user.users.password);
		    		 
		    		 alert("返回的bean:"+user.noticeBean.id+":"+user.noticeBean.title+":"+user.noticeBean.content);
		    		 
		    		 $.each(user.list,function(num,value){   
					    $("#result").append("<div>welcome您,"+user.users.username+"</div>").append("<div>第"+(num+1)+"个用户:</div>")   
					       .append("<div><font color='red'>用户ID:"+value.id+"</font></div>")   
					          .append("<div><font color='red'>公告标题:"+value.title+"</font></div>")   
					          .append("<div><font color='red'>公告内容:"+value.content+"</font></div>");   
					});
		    		$("#login").attr("disabled",true);
		    	
		    	},
		    	/*
		    	success: function(data){
		    		//alert("123");  
		    		//alert(data);  
                    //alert("您已预约成功!"); 
                    alert(data);
                    
                    
                    var user = eval("("+data+")");//  
		        
		            //alert("user:"+user);  
		          
		            $("#result").each(function(){  
		                $(this).html('welcome ,' + user[0].username);  
		            }); 
                    $("#login").attr("disabled", true);  
                },
                */
                error: function(){  
                    alert("您的预约信息保存失败,请重新预约!");