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

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

问题描述

我正在尝试使用Spring Boot重写配置繁重的原始Spring MVC项目.我使用Spring Boot Initiaizer在IntelliJ中启动了一个全新的Spring Boot项目,并且正在尝试基于Java的最小配置.许多教程指出,生成的默认主类就足够了,并且@SpringBootApplication包含了所有内容.我有一个示例REST Controller可以工作并以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天全站免登陆