spring boot 使用视图modelandview

原文:https://www.cnblogs.com/liyafei/p/7955943.html

1:springboot使用视图解析器,添加依赖

        <!-- freemarker模板引擎视图 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!-- 热部署,不用重启 ,这个在这里不需要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- jsp解析器 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

2:主函数需要继承SpringBootServletInitializer,并覆盖其方法。

package com.liyafei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; @EnableAutoConfiguration
@SpringBootApplication
//返回jsp页面必须继承SpringBootServletInitializer类重写里面的方法
public class Main extends SpringBootServletInitializer{ public static void main(String[] args) {
SpringApplication.run(Main.class, args); } protected SpringApplicationBuilder config(SpringApplicationBuilder applicationBuilder){
return applicationBuilder.sources(Main.class);
}
}

3:配置文件中添加spring.mvc.view配置,配置了视图解析器之后,controlller返回的String,View等就会先找视图解析器

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo
username: root
password: 1367356
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp mybatis:
config-location: classpath:mybatis-config.xml

4:controller映射

  

package com.liyafei.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import com.liyafei.pojo.User; //这个注解不能使用RestController,不然会返回模板类型的页面
@Controller
public class MyController { User user=new User();
@RequestMapping("/my")
public ModelAndView test(){
ModelAndView mv=new ModelAndView();
mv.setViewName("modelandview");
mv.addObject("name", "liyafei");
user.setAge(20);
user.setName("wangwu");
mv.addObject("user", user); //设置返回的数据为json类型,也可以不设置,返回对象
//mv.setView(new MappingJackson2JsonView());
return mv;
}
@RequestMapping("index")
public String index(){
return "index";
}
}

5:测试成功:

6:目录结构

SpringBoot使用ModelAndView时配置视图解析器的更多相关文章

  1. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  2. SpringMVC中ModelAndView对象与“视图解析器”

    摘要: spring MVC这个环境中,Spring MVC会依据controller(或者你叫它handler)中处理方法的返回值,进行解析,解析之后提供一个视图,作为响应. 标注了@Control ...

  3. SpringMVC什么时候配置 视图解析器

    当Action返回的是一个真实路径的时候,视图解析器可不进行配置 当Action返回的是逻辑路径的时候,我们必须要在配置文件中注册视图解析器并为该逻辑路径添加前缀和后缀

  4. SpringBoot应用配置常用相关视图解析器

    目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...

  5. spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器

    创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...

  6. SpringMVC 视图和视图解析器&表单标签

    视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它 ...

  7. 【SpringMVC】SpringMVC系列10之视图与视图解析器

    10.视图与视图解析器 10.1.概述     请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...

  8. 【SpringMVC笔记】第五课 改进Handler处理器和视图解析器

    第四课 已经对注解的映射器和适配器进行了改进. 接下来需要对Handler处理器和视图解析器进行改进. <!-- 配置handler处理器 --> <bean class=" ...

  9. SpringMVC——视图和视图解析器

    请求处理方法执行完成后,最终返回一个 ModelAndView对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个Mode ...

随机推荐

  1. Grunt教程——安装Grunt

    Grunt教程--安装Grunt 作者:大漠 日期:2013-11-04 点击:3124 tools grunt 在上一节<Grunt教程--初涉Grunt>一文中介绍了Grunt是什么, ...

  2. [C#]做服务使用Process启动外部程序没窗体

    这几天会到一个需要,要时时侦测文件生成,并上传到Server上,侦测文件生成使用的FileSystemWatch.但是时时运行遇到了问题,程序可能会人为退出或者意外终止,使用一个进程监控程序的监程,也 ...

  3. [ASP.NET]使用Layer简介

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 在与同类组件的比较中,layer总是能轻易获胜.她尽可能 ...

  4. python的数字图像处理学习(2)

    图像的重定义大小,图像的缩扩,图像的旋转: from skimage import transform,data import matplotlib.pyplot as plt img = data. ...

  5. linux安装dpkg安装缺少依赖项的解决

    问题: dpkg: error processing package rxvt:i386 (--install): dependency problems - leaving unconfigured ...

  6. transform.forward和vector3.forward

    Vector3.forward的值永远是(0,0,1)(这里的(0,0,1)是世界坐标的(0,0,1)),而transform.forward我们可以理解为其对应物体的z轴方向,是一个向量,而不是一个 ...

  7. 前端之html的常用标签2和css基本使用

    一 列表标签 ul标签:无序列表 ol标签:有序列表 li标签:写在ul和ol标签里面的 dl标签:定义列表 dt标签和dd标签:都写在dl里面的 <!DOCTYPE html> < ...

  8. (7)Why 30 is not the new 20

    https://www.ted.com/talks/meg_jay_why_30_is_not_the_new_20/transcript 00:12When I was in my 20s, I s ...

  9. tflite笔记

    固化模型 方法一:freeze_graph方法 把tf.train.write_graph()生成的pb文件与tf.train.saver()生成的chkp文件固化之后重新生成一个pb文件 with ...

  10. java常用设计模式十一:策略模式

    一.概述 定义:策略模式是指对一系列的算法定义,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(概念不好理解,可以看第二节的合例子) 基本角色: 环境( ...