目录
SpringBoot 的自动装配装配了视图解析器了吗?
SpringBoot 使用 JSP
SpringBoot 中使用 Thymeleaf
SpringBoot 中使用 Freemark
SpringBoot 的自动装配装配了视图解析器了吗?
我们可以看到 SpringBoot 自动装配的 webMvcAutoConfiguration 类中, 装配了以下关于 ViewResolver(视图解析器) 的类. 可以看到 SpringBoot 已经自动装配了 InternalResourceViewResolver 类, 又是通过外部资源配置的方式来配置此视图解析器 this.mvcProperties.getView().getPrefix(), 所以我们可以在 application.properties 文件配置此视图解析器用于解析 JSP.
- @Bean
- @ConditionalOnMissingBean
- public InternalResourceViewResolver defaultViewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix(this.mvcProperties.getView().getPrefix());
- resolver.setSuffix(this.mvcProperties.getView().getSuffix());
- return resolver;
- }
SpringBoot 使用 JSP
SpringBoot 在自动装配的时候默认就已经将 JSP 的视图解析器 InternalResourceViewResolver 装配. 所以我们只需要进行配置使用即可. 在 SpringBoot 中使用 JSP 比较麻烦一点, 或许是我的个人理解存在什么误区, 如果有朋友知道更好的配置方法, 请留言给我.
第一步: 创建自定义 webapp 目录, 如下所示
第二步: 将此文件夹配置成项目的 Web 模块
第三步: 导入 JSP 相关依赖
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- </dependency>
第四步: 在 SpringBoot 的属性文件 application.properties 中配置 JSP 的路由
- spring.mvc.view.prefix=/
- spring.mvc.view.suffix=.jsp
第五步: 修改 Maven 的 pom.xml 文件打包方式改成 war(默认打包 Jar, 打包 Jar 包的方式使用 Idea 启动是没什么问题, 如果单独运行 Jar 包就找不到 JSP 文件, 如果改成 War 包即可)
<packaging>war</packaging>
SpringBoot 中使用 Thymeleaf
SpringBoot 官方是推荐使用 thymeleaf 作为优选的视图解析器, 所以 SpringBoot 对 Thymeleaf 的支持非常好, 这里仅仅演示 SpringBoot 如何选用 Thymeleaf 作用默认视图解析器.
第一步: 导入 Thymeleaf 的依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
第二步: 创建存放 Thymeleaf 模板文件夹, 在 Resources 目录下创建 templates 目录
这个文件夹的名字可不是我么随便命名的啊, 是 SpringBoot 在自动装配 Thymeleaf 视图解析器的时候就已经预定义好了, 我们看一下它的定义源码.
- @ConfigurationProperties(prefix = "spring.thymeleaf")
- public class ThymeleafProperties {
- private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
- public static final String DEFAULT_PREFIX = "classpath:/templates/";
- public static final String DEFAULT_SUFFIX = ".html";
- }
SpringBoot 中使用 Freemark
第一步: 导入 Maven 依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
第二步: 创建存放 Freemark 模板文件夹, 在 Resources 目录下创建 templates 目录
- @ConfigurationProperties(prefix = "spring.freemarker")
- public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
- public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
- public static final String DEFAULT_PREFIX = "";
- public static final String DEFAULT_SUFFIX = ".ftl";
- }
我们可以看到 SpringBoot 在自动装配 Freemarker 视图解析器默认是将模板文件放在 classpath:/templates / 路径内, 我们同样可以在 SpringBoot 的配置文件中自行配置.
小提示: 我在写 Freemark 视图解析器的时候并没有将第一个 JSP 内部资源解析器给删除掉, 所以他们是并存的, 所以我们可以知道 SpringBoot 在装配他们的时候给予设定了优先级顺序. 从下图可以看到他们的优先级顺序; Freemarker>Thymeleaf>InternalResourceViewResolver`
该教程所属 Java 工程师之 SpringBoot 系列教程, 本系列相关博文目录 Java 工程师之 SpringBoot 系列教程前言 & 目录
来源: https://www.cnblogs.com/jimisun/p/10092137.html