默认情况下,Spring Boot 期望视图存储在哪里? [英] By default, where does Spring Boot expect views to be stored?

查看:36
本文介绍了默认情况下,Spring Boot 期望视图存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring Boot 重写我的配置繁重的普通 Spring MVC 项目.我使用 Spring Boot Initiaizer 在 IntelliJ 中启动了一个全新的 Spring Boot 项目,我将采用基于 Java 的最小配置的路线.许多教程指出生成的默认主类就足够了,并且 @SpringBootApplication 包含了所有内容.我得到了一个示例 REST 控制器来工作并以 JSON 形式返回一个序列化的对象,但似乎很难显示一个视图.我的结构如下,除了我创建的 webapps 目录外,其他所有东西都是默认的.

I'm experimenting on re-writing my configuration-heavy, vanilla Spring MVC project using Spring Boot. I started a brand new Spring Boot project in IntelliJ using the Spring Boot Initiaizer and I'm going the route of minimal Java-based configuration. Lots of tutorials point out that the default main class generated is sufficient and that @SpringBootApplication has everything included. I got a sample REST Controller to work and return a serialized object as JSON, but it appears getting a view to show is proving difficult. My structure is as follows, with everything default other than the webapps directory which I created.

src
`-main
   `-java
   `-resources
     `-static
     `-templates
   `-webapp
     `-WEB-INF
        `-home.jsp

控制器很简单.

@Controller
public class ViewMaster {
    @RequestMapping("/home")
    public String home() {
        return "home";
    }
}

没有任何配置,我想知道 Spring Boot 期望将视图存储在哪里以及使用什么扩展名(html?).我还尝试在 application.properties 中包含以下内容,但我仍然收到 404 错误.移动 WEB-INF 目录或仅移动资源中的 html 文件也无济于事.

Without any configuration, I'd like to know where Spring Boot expects the views to be stored and with what extension (html?). I've also tried to include the following in application.properties but I still get a 404 error. Moving the WEB-INF directory or just the html file around in resources didn't help either.

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

我也试过在我的 pom.xml 中包含这些依赖项,但没有任何影响.

I've also tried including these dependencies in my pom.xml without any effect.

<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>

我一定在这里遗漏了一些非常明显的东西,所以如果有人能指出这一点,不胜感激!

I must be missing something painfully obvious here so appreciate if someone can point that out!

推荐答案

解决方案

我通过反复试验找到了答案,结果很烦人.如果这个结论是错误的,我希望有人能纠正我,但似乎 Spring Boot 不喜欢字符串 WEB-INF.我将 WEB-INF 目录重命名为 view 并将 application.properties 更改为以下内容,视图加载成功.

I found the answer via trial-and-error, which turned out rather annoying. I hope someone can correct me if this conclusion is wrong, but it appears that Spring Boot does not like the string WEB-INF. I renamed the WEB-INF directory to view and changed the application.properties to the following and the view loaded successfully.

spring.mvc.view.prefix=/view/
spring.mvc.view.suffix=.jsp

其他发现

本练习的目的是创建一个基于 Java 的最小配置的工作示例,因此我继续最小化设置.然后我发现在多个 SO 主题和论坛上提出的很多建议都没有帮助.@JBNizet 在他对 Spring Boot 文档的评论中提供了一个链接,其中列出了一个没有人提到的非常突出的观点:JSP 根本无法与 Spring Boot 配合使用,因为它具有取决于所选嵌入式容器的限制.考虑到这一点,我决定尝试用 ThymeLeaf 模板替换 JSP.

The objective of this exercise was to create a working example of a minimal, Java-based configuration so I continued minimalising the setup. I then found that lots of advice dished out on multiple SO threads and forums did not help. @JBNizet provided a link in his comment to the Spring Boot docs which lists a very salient point that no one has mentioned: JSPs simply do not play well with Spring Boot as it has limitations depending on the embedded container chosen. With that in mind, I decided to try replacing JSPs with ThymeLeaf templates.

我的新工作配置不需要这些:

My new working config removes the need for these:

  • 无需添加application.properties:spring.mvc.view.prefix + spring.mvc.view.suffix
  • 无需将包装类型从 jar 更改为 war
  • 无需修改主类
  • 无需为 pom.xml 添加依赖项
    • org.springframework.boot/spring-boot-starter-tomcat
    • org.springframework.boot/tomcat-embed-jasper
    • javax.servlet/jstl
    • No need to add application.properties: spring.mvc.view.prefix + spring.mvc.view.suffix
    • No need to change the packaging type from jar to war
    • No need to modify main class
    • No need to add pom.xml dependencies for
      • org.springframework.boot / spring-boot-starter-tomcat
      • org.springframework.boot / tomcat-embed-jasper
      • javax.servlet / jstl

      所以只是默认的 Spring Boot 模板和 2 个 ThymeLeaf 依赖项,视图名为 ViewName.html 放在 src/main/resources/templates 中.

      So just the default Spring Boot template and 2 ThymeLeaf dependencies with the views named as ViewName.html placed in src/main/resources/templates.

      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf-spring4</artifactId>
      </dependency>
      

      这篇关于默认情况下,Spring Boot 期望视图存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆