SpringMVC中的返回值问题之三返回list类型和map类型
web.xml文件
<!DOCTYPE web-app PUBLIC
                "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
  <!--编码过滤器-->
  <filter>
    <filter-name>CharactorEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharactorEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--配置前端控制器-->
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--初始化参数-->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc07RetuenObject.xml</param-value>
  </init-param>
  <!--意思:Tomact启动,就将servlet创建好放入内存中了-->
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     ">
    <!--扫描包-->
    <context:component-scan base-package="cn.sjl.day04returnobject"></context:component-scan>
    <!--访问静态资源-->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <!--注解驱动-->
    <mvc:annotation-driven/>
</beans>
返回list集合类型

//控制器
@Controller
public class FirstController {
    /**
     * 返回list集合类型
     * @return
     */
    @RequestMapping("/third")  //控制器方法(即访问方法)
    @ResponseBody     //响应体
    public Object doThird(){
        List<Student> list=new ArrayList<Student>();
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);
        Student student1=new Student();
        student1.setName("张三");
        student1.setAge(20);
        list.add(student);
        list.add(student1);
        return list;
    }
}
return.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<html>
<head>
    <title>返回值数值</title>
</head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
     $(function(){
         $("#btn").click(function(){
             $.ajax({
                 url:"${pageContext.request.contextPath}/third",
                 success:function (data) {
                     //遍历list集合
                     $.each(data,function (i,dom) {
                         alert(dom.name);
                     });
                 }
             });
         });
     });
</script>
<body>
<input type="button" id="btn" value="Ajax"/>
</body>
</html>
访问方式

返回Map集合类型

public class Student {
private String name;
private Integer age;
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@Controller
public class FirstController {
    /**
     * map类型
     * @return
     */
    @RequestMapping("/four")  //控制器方法(即访问方法)
    @ResponseBody     //响应体
    public Object doFour(){
        Map<String,Student> map=new HashMap<String, Student>();
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);
        map.put(student.getName(),student);
        Student stu=new Student();
        stu.setName("张三2");
        stu.setAge(25);
        map.put(stu.getName(),stu);
        return map;
    }
}
return.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<html>
<head>
    <title>返回值数值</title>
</head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
     $(function(){
             $("[type=button]").click(function(){
             $.ajax({
                 url:"${pageContext.request.contextPath}/four",
                 success:function (data) {
                     //遍历map集合
                     $.each(data,function (i,dom) {
                         alert(dom.name);
                     });
                 }
             });
         });
     });
</script>
<body>
<input type="button" id="btn" value="Ajax"/>
</body>
</html>
 
 
 
 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号