1. 1.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> 2 <html> 3 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jQuery1.11.1.js"></script> 4 <script type="text/javascript"> 5 $(function(){ 6 $("#btn").click(function(){ 7 $.ajax({ 8 url:"${pageContext.request.contextPath}/five", 9 success:function(data){ //data指的是从server打印到浏览器的数据 10 /* $.each(data,function(i,dom){ 11 alert(dom.name); 12 })*/ 13 14 $.each(data,function(i,dom){ 15 alert(dom.name); 16 }) 17 } 18 }); 19 }); 20 }); 21 22 </script> 23 <body> 24 <h2>ResponseBody</h2> 25 <%--<form action="" method="post">--%> 26 <input type="submit" id="btn" value="Ajax"/><%--</form>--%> 27 </body> 28 </html>
2.
1 @Controller 2 public class FirstController { 3 @RequestMapping("/first") 4 @ResponseBody 5 public Object doFirst(){ 6 return 1; 7 } 8 @RequestMapping(value ="/second",produces = "text/html;charset=utf-8") 9 @ResponseBody 10 public Object doSecond(){ 11 return "就业拼了"; 12 } 13 @RequestMapping(value = "/third") 14 @ResponseBody 15 public Object doThird(){ 16 UserInfo info=new UserInfo(); 17 info.setName("白白"); 18 info.setAge(22); 19 return info; 20 } 21 22 23 @RequestMapping(value = "/four") 24 @ResponseBody 25 public Object doFour(){ 26 Map<String,UserInfo> map=new HashMap<String, UserInfo>(); 27 UserInfo info=new UserInfo(); 28 info.setName("白白"); 29 info.setAge(20); 30 31 UserInfo info2=new UserInfo(); 32 info2.setName("小熊啊"); 33 info2.setAge(22); 34 map.put(info.getName(),info); 35 map.put(info2.getName(),info2); 36 return map; 37 } 38 @RequestMapping(value = "/five") 39 @ResponseBody 40 public Object doFive(){ 41 List<UserInfo> list=new ArrayList<UserInfo>(); 42 UserInfo info=new UserInfo(); 43 info.setName("白白"); 44 info.setAge(22); 45 list.add(info); 46 return list; 47 } 48 49 }
3.
1 public class UserInfo { 2 private String name; 3 private Integer age; 4 }
4.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/mvc 16 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 17 <context:component-scan base-package="cn.happy.controller"></context:component-scan> 18 <mvc:annotation-driven/> 19 </beans>
时间: 2024-10-10 11:37:11