码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC参数传递方式list,map

时间:2014-12-31 14:34:41      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:spring mvc   maven   web   java   

 本文是基于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="/test" )
public class DemoController {
     @RequestMapping(value= "/param")
     public String   testParam(Model model ){
           User u1 = new User();
            u1.setUserId(1);
            u1.setPassword( "123456");
            u1.setUserName( "张三");
            model.addAttribute( "user", u1);
            return "param" ;
     }
}


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="/test" )
public class DemoController {
     @RequestMapping(value= "/param")
     public String  testParam(Model model){
           
            return mapInfo();
     }
     /*
      * ModelAndView:它代表了Spring Web MVC中呈现画面时所使用的Model与View,
      * 由于Java一次只能返回一个物件,所以ModelAndView的作用封装这两个物件,以方便您一次返回Model与View这两个物件。
      * @return
      */
     private ModelAndView mapInfo(){
           Map<String,Object> map = new HashMap<String, Object>(); 
           User u1 = new User();
            u1.setUserId(1);
            u1.setPassword( "123456");
            u1.setUserName( "张三");
           
           User u2 = new User();
            u2.setUserId(1);
            u2.setPassword( "123456");
            u2.setUserName( "张四");
           
            map.put( "user1", u1);
            map.put( "user2", u2);
            /*
            * 1、" param":试图名,即跳转到param.jsp页面
            * 2、"map":传递的对象为map类型
            * 3、map:被传递的对象
            */
            return new ModelAndView( "param", "map", map);
     }
}
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="/test" )
public class DemoController {
     @RequestMapping(value= "/param")
     public String  testParam(Model model){
            model.addAttribute( listInfo());
            return "param";
     }
     public @ModelAttribute List<User> listInfo(){
           List<User> list = new ArrayList<User>();
           User u1 = new User();
            u1.setUserId(1);
            u1.setPassword( "123456");
            u1.setUserName( "张三");
           
           User u2 = new User();
            u2.setUserId(1);
            u2.setPassword( "123456");
            u2.setUserName( "张四");
           
           User u3 = new User();
            u3.setUserId(1);
            u3.setPassword( "123456");
            u3.setUserName( "张五");
           
            list.add( u3);
            list.add( u2);
            list.add( u1);
            return list;
     }
}
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

标签:spring mvc   maven   web   java   

原文地址:http://blog.csdn.net/chonggaoing/article/details/42294095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!