SpringBoot使用ModelAndView时配置视图解析器
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时配置视图解析器的更多相关文章
- spring boot 开静态资源访问,配置视图解析器
配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...
- SpringMVC中ModelAndView对象与“视图解析器”
摘要: spring MVC这个环境中,Spring MVC会依据controller(或者你叫它handler)中处理方法的返回值,进行解析,解析之后提供一个视图,作为响应. 标注了@Control ...
- SpringMVC什么时候配置 视图解析器
当Action返回的是一个真实路径的时候,视图解析器可不进行配置 当Action返回的是逻辑路径的时候,我们必须要在配置文件中注册视图解析器并为该逻辑路径添加前缀和后缀
- SpringBoot应用配置常用相关视图解析器
目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...
- spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器
创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...
- SpringMVC 视图和视图解析器&表单标签
视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它 ...
- 【SpringMVC】SpringMVC系列10之视图与视图解析器
10.视图与视图解析器 10.1.概述 请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...
- 【SpringMVC笔记】第五课 改进Handler处理器和视图解析器
第四课 已经对注解的映射器和适配器进行了改进. 接下来需要对Handler处理器和视图解析器进行改进. <!-- 配置handler处理器 --> <bean class=" ...
- SpringMVC——视图和视图解析器
请求处理方法执行完成后,最终返回一个 ModelAndView对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个Mode ...
随机推荐
- python学习 day12 (3月18日)----(装饰器内置函数)
读时间函数: # import time # def func(): # start_time = time.time() # 代码运行之前的时间 # print('这是一个func函数') # ti ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
- java.security.cert.CertificateException: No subject alternative names matching IP address xxx.xxx.xxx.xxx found
https与http不同的是,https加密,需要验证证书,而http不需要. 在连接的代码中加上: static { disableSslVerification(); } private stat ...
- 最全js表单验证
/***************************************************************** 表单校验工具类 (linjq) ***************** ...
- vue中的前置守卫
前置守卫是为了验证用户信息真实性,一些内容只能在用户登陆以后才能进行查看,例如个人中心,我的购物车,等个人页面,非隐私页面 用router.beforeEach进行验证,这个方法必须写在router实 ...
- Keras分类问题
#-*- coding: utf-8 -*- #使用神经网络算法预测销量高低 import pandas as pd #参数初始化 inputfile = 'data/sales_data.xls' ...
- max10之pll时钟源切换
问题3:PLL切换功能中,多次切换可能造成PLL锁不定 从现象看clkbadx信号是不影响的,但locked信号一定是有影响的.
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十一之铭文升级版
铭文一级: 第8章 Spark Streaming进阶与案例实战 黑名单过滤 访问日志 ==> DStream20180808,zs20180808,ls20180808,ww ==> ( ...
- Python 之自动获取公网IP
Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...
- spring之hello(简单环境配置)
导入java包 配置springmvc.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans ...