标签:spring mvc maven web java
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 @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" ; } }
<%@ 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>
@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); } }
<%@ 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>
@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; } }
<%@ 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>
标签:spring mvc maven web java
原文地址:http://blog.csdn.net/chonggaoing/article/details/42294095