springmvc 之 ModelAndView的简单使用

转自:https://www.cnblogs.com/Sunnor/p/6130380.html

 参考:http://blog.csdn.net/zzjjiandan/article/details/34089313

先上图:

springmvc 之 ModelAndView的简单使用_第1张图片

 

MAVTest.java

复制代码
 1 package com.wyl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 /**
13  * ModelAndView测试,
14  * 
15  * @author Wei
16  * @time 2016年12月4日 上午10:12:16
17  */
18 @Controller
19 public class MAVTest {
20 
21     public MAVTest() {
22         // TODO Auto-generated constructor stub
23     }
24 
25     @RequestMapping(value = "login")
26     public ModelAndView login() {
27         System.out.println("MAVTest.java login()....");
28         ModelAndView mav = new ModelAndView();
29         mav.setViewName("welcome");
30         mav.addObject("msg", "hello kitty");
31 
32         // List
33         List list = new ArrayList();
34         list.add("java");
35         list.add("c++");
36         list.add("oracle");
37         mav.addObject("bookList", list);
38 
39         // Map
40         Map map = new HashMap();
41         map.put("zhangsan", "北京");
42         map.put("lisi", "上海");
43         map.put("wangwu", "深圳");
44         mav.addObject("map", map);
45 
46         return mav;
47     }
48 
49 }
复制代码

 

welcome.jsp

复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>welcome页面title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     
22   head>
23   
24   <body>  
25      
26    ${msg } <br/>  
27      
28    <p>书籍列表p>  
29    <c:forEach items="${bookList}" var="node">  
30         <c:out value="${node}">c:out>  
31    c:forEach>  
32    <br/>  
33    <br/>  
34      
35      
36    <c:forEach items="${map}" var="node">  
37         姓名:<c:out value="${node.key}">c:out>  
38         住址:<c:out value="${node.value}">c:out>  
39         <br/>  
40    c:forEach>  
41 body>  
42 html>
复制代码

注意:c标签所依赖的jar,jstl.jar,commons-el.jar,standard.jar

项目结构:

springmvc 之 ModelAndView的简单使用_第2张图片

 

你可能感兴趣的:(SpringMVC)