首页 > 代码库 > SpringMVC参数传递方式list,map

SpringMVC参数传递方式list,map

 本文是基于SpringMVC注解的方式来实现页面与后台之间参数的传递,本文主要侧重于从后台传递到前台view。
内容:
1、后台传递单个对象User到前台;
2、后台传递集合对象到前台(分Map和List两种情形)
下面的内容基于SpringMVC工程配置已经准备好,该工程是用maven搭建的。
一:传递单个对象(以用户对象User为例)
User类:
public class User {
     private Integer userId;
     //用户名
     private String userName;
     //密码
     private String password;
     public Integer getUserId() {
            return userId;
     }
     public void setUserId(Integer userId) {
            this. userId = userId;
     }
     public String getUserName() {
            return userName;
     }
     public void setUserName(String userName) {
            this. userName = userName;
     }
     public String getPassword() {
            return password;
     }
     public void setPassword(String password) {
            this. password = password;
     }
}

Controller类:
@Controller
@RequestMapping(value=http://www.mamicode.com/"/test" )>

param.jsp页面:
<%@ page language ="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
     <body >
    用户名:${user.userName}
    密码:${user.password}
     </body >
</html>
二:传递Map集合对象(以Map<String,User>为例)
User类同上;
Controller类:
@Controller
@RequestMapping(value=http://www.mamicode.com/"/test" )>
param.jsp页面:
<%@ page language ="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
     <body >
           <c:forEach items ="${requestScope.map}" var= "m"> 
             ${m.key} : ${m.value.userName} <br /> 
          </c:forEach >
     </body >
</html>
三:传递List集合对象(以List<User>为例)
User类同上;
Controller类:
@Controller
@RequestMapping(value=http://www.mamicode.com/"/test" )>
param.jsp页面:
<%@ page language ="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
     <body >
         <c:forEach items ="${requestScope.userList}" var= "m"> 
             用户名:${m.userName};密码:${m.password} <br /> 
         </c:forEach >
      </body >
</html>



SpringMVC参数传递方式list,map