2022-08-20

Spring MVC应用中,我们经常需要在Controller将数据传递到JSP页面,除了可以通过HttpServletRequest域传递外,Spring MVC还提供了两个Api,分别为Model接口和ModelMap类。接下来看看如何使用?

1 编写控制器数据存入域

            
            package com.yiidian.controller;
            import org.springframework.stereotype.Controller;
            import org.springframework.ui.Model;
            import org.springframework.ui.ModelMap;
            import org.springframework.web.bind.annotation.RequestMapping;
            
            /**
             *  Model与ModelMap的使用
             * 一点教程网 - www.yiidian.com
             */
            @Controller
            public class ModelController {
            
                /**
                 * Model接口的使用
                 * @return
                 */
                @RequestMapping("/model")
                public String list(Model model){
                    model.addAttribute("model","Model-一点教程网");
                    return "success";
                }
            
                /**
                 * ModelMap类的使用
                 * @return
                 */
                @RequestMapping("/modelMap")
                public String list(ModelMap modelMap){
                    modelMap.addAttribute("modelMap","ModelMap-一点教程网");
                    return "success";
                }
            }

2 编写JSP获取域数据

            
            <%@ page contentType="text/html;charset=UTF-8" language="java" %>
            <html>
            <head>
                <title>一点教程网-提示页面</title>
            </head>
            <body>
            获取Model数据-${requestScope.model}
            <hr/>
            获取ModelMap数据-${requestScope.modelMap}
            </body>
            </html>

3 springmvc.xml配置

            
            <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
            
                <!-- 1.扫描Controller的包-->
                <context:component-scan base-package="com.yiidian.controller"/>
            
                <!-- 2.配置视图解析器 -->
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <!-- 2.1 页面前缀 -->
                    <property name="prefix" value="/pages/"/>
                    <!-- 2.2 页面后缀 -->
                    <property name="suffix" value=".jsp"/>
                </bean>
            
                <!-- 3.创建处理器适配器和处理器映射器-->
                <mvc:annotation-driven/>
            </beans>

4 运行测试

访问:http://localhost:8080/model,效果如下:

202202131334533551.png

访问:http://localhost:8080/modelMap,效果如下:

202202131334538832.png

5 Model与ModelMap的关系

其实Model接口和ModelMap类都有一个共同的子类:BindingAwareModelMap

202202131334544043.png

202202131334551224.png

而BindingAwareModelMap底层其实是往HttpServletRequest域存入数据,所以Model接口或者ModelMap的底层也是往request域存入数据!

源码下载:https://pan.baidu.com/s/14vlnvgAwiTsRre7WG1s7FQ

阅读全文