作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
Spring整合JSON

Custom Tab

在使用SpringMVC结合JSON时,需要导入com.springsource.org.codehaus.jackson.mapper-1.4.2.jar和com.springsource.org.codehaus.jackson-1.4.2.jar两个包

直接实例:

 <script type="text/javascript" src="source/jquery.js"></script>
    <script type="text/javascript">
        
        $(document).ready(function(){
        alert({"name":"zhangsan","age":10});
         alert(JSON.stringify({"user":"zhangsan","age":10}));
           $("#t").blur(function(){
                $.ajax({
                    url:"json",
                    type:"POST",
                    dataType:"json",
                    data:{"name":"zhangsan","age":10,"sex":"meal"},
                    success:function(data,status)
                    {
                          $("#s").html(data.name);
                          
                    },
                    error:function()
                    {
                       alert("fail");
                    }
                
                });
            
           });
           
        });
    
    
    
    
    </script>
    
    
  </head>
  
  <body>
      <input type="text" id="t" /><span id="s"></span>
      
  </body>
</html>
@Controller
public class SpringMVCJson {
   
// @ResponseBody
@RequestMapping("/json")
public User json(@RequestBody User user, HttpServletRequest request,HttpServletResponse response) throws IOException
{

System.out.println(user.getAge());

System.out.println(request.getParameter("age"));
 
response.getWriter().print("{\"name\":\""+user.getName()+"\"}");

// return "{\"username\":\""+user.getName()+"\"}";


user.setName("helloworld");

//   return user;

return null;
}


}

转载自:http://blog.csdn.net/u013516966/article/details/40714617

Home