首页 > 代码库 > springmvc的ModelAndView的简单使用

springmvc的ModelAndView的简单使用

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

先上图:

技术分享

 

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 = "http://www.mamicode.com/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<String> list = new ArrayList<String>();
34         list.add("java");
35         list.add("c++");
36         list.add("oracle");
37         mav.addObject("bookList", list);
38 
39         // Map
40         Map<String, String> map = new HashMap<String, String>();
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     <!--
20     <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
21     -->
22   </head>
23   
24   <body>  
25    <!-- 输出普通字符 -->  
26    ${msg } <br/>  
27    <!-- 输出List -->  
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    <!-- 输出Map -->  
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

项目结构:

技术分享

 

 

项目见:

 链接:http://pan.baidu.com/s/1sllfet3 密码:j27h

 

springmvc的ModelAndView的简单使用