Spring Boot 使用带有 JSP 模板的资源模板文件夹而不是 webapp 文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46183633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:30:39  来源:igfitidea点击:

Spring boot use resources templates folder with JSP templates instead of webapp folder?

springjspspring-mvctemplatesspring-boot

提问by xetra11

I started a Spring Boot MVC project and realized that there are two folder within resources. One is called templatesand the other static. I really like this folder setup.

我启动了一个 Spring Boot MVC 项目,并意识到resources. 一个被称为templates另一个static。我真的很喜欢这个文件夹设置。

The problem is that I use JSP Templates for my views. I could not place a .jsptemplate inside the templatesfolder and got it to work. What I needed to do is to create a webappfolder on the same level as srcand resources. Placing my JSP templates in there and then my views can be found.

问题是我的视图使用 JSP 模板。我无法.jsptemplates文件夹中放置模板并使其工作。我需要做的是创建一个webappsrc和相同级别的文件夹resources。将我的 JSP 模板放在那里,然后可以找到我的视图。

What do I need to reconfigure to actually use my JSP templates within the templatesfolder which lies within resources?

我需要重新配置什么才能在templates位于 内的文件夹中实际使用我的 JSP 模板resources

采纳答案by Franz Fellner

According to the Maven documentationsrc/main/resourceswill end up in WEB-INF/classesin the WAR.

根据Maven 文档src/main/resourcesWEB-INF/classes在 WAR 中结束。

This does the trick for Spring Boot in your application.properties:

这对您的 Spring Boot 有用application.properties

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

If you prefer Java configuration this is the way to go:

如果你更喜欢 Java 配置,这是要走的路:

@EnableWebMvc
@Configuration
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/classes/templates/");
        bean.setSuffix(".jsp");
        return bean;
    }
}

Update with a full example

更新一个完整的例子

This example was based on Spring's initializer(Gradle project with "Web" dependency). I just added apply plugin: 'war'to the build.gradle, added/changed the files below, built teh project with gradle warand deployed it to my application server (Tomcat 8).

此示例基于Spring 的初始化程序(具有“Web”依赖项的 Gradle 项目)。我刚刚添加apply plugin: 'war'build.gradle,添加/更改了下面的文件,使用gradle war它构建项目并将其部署到我的应用程序服务器(Tomcat 8)。

This is the directory tree of this sample project:

这是此示例项目的目录树:

\---src
    +---main
        +---java
        |   \---com
        |       \---example
        |           \---demo
        |                   ApplicationConfiguration.java
        |                   DemoApplication.java
        |                   DemoController.java
        |
        \---resources
            +---static
            \---templates
                    index.jsp

ApplicationConfiguration.java: see above

ApplicationConfiguration.java:见上文

DemoApplication.java:

DemoApplication.java:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoApplication.class, args);
    }

}

DemoController.java:

演示控制器.java:

@Controller
public class DemoController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

index.jsp:

索引.jsp:

<html>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

回答by oak

Official information:

官方信息:

Resource handling:

资源处理:

Links to resources are rewritten at runtime in template, thanks to a ResourceUrlEncodingFilter, auto-configured for Thymeleaf and FreeMarker. You should manually declare this filter when using JSPs. source

由于 ResourceUrlEncodingFilter 为 Thymeleaf 和 FreeMarker 自动配置,因此在运行时在模板中重写了资源链接。使用 JSP 时,您应该手动声明此过滤器。 来源

Supported template engine

支持的模板引擎

As well as REST web services, you can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies including Thymeleaf, FreeMarker and JSPs.

除了 REST Web 服务,您还可以使用 Spring MVC 来提供动态 HTML 内容。Spring MVC 支持多种模板技术,包括 Thymeleaf、FreeMarker 和 JSP。

[...]

[...]

JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers.

如果可能,应该避免使用 JSP,在将它们与嵌入式 servlet 容器一起使用时存在一些已知的限制。

[..]

[..]

When you're using one of these templating engines with the default configuration, your templates will be picked up automatically from src/main/resources/templates.

当您使用具有默认配置的这些模板引擎之一时,您的模板将自动从 src/main/resources/templates 中选取。

source

来源

Spring boot JSP limitations

Spring boot JSP 限制

  • With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard
    container (not limited to, but including Tomcat).
  • An executable jar will not work because of a hard coded file pattern in Tomcat.
  • With Jetty it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to any standard container.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page won't override the default view for error handling, custom error pages should be used instead.
  • 使用 Tomcat 时,如果您使用 war 打包,它应该可以工作,即可执行的 war 可以工作,并且还可以部署到标准
    容器(不限于,但包括 Tomcat)。
  • 由于 Tomcat 中的硬编码文件模式,可执行 jar 将无法工作。
  • 使用 Jetty,如果您使用 war 打包,它应该可以工作,即可执行的 war 将工作,并且也可以部署到任何标准容器。
  • Undertow 不支持 JSP。
  • 创建自定义 error.jsp 页面不会覆盖错误处理的默认视图,而应使用自定义错误页面。

source

来源

Technical change

技术变化

Tell spring boot to from where to load the JSP files. In application.propertiesset

告诉 spring boot 从哪里加载JSP files. 在application.properties

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp

source

来源

Sample spring boot with JSP

带有 JSP 的示例弹簧靴

In case you do want to use JSPwith spring boot here are two examples:

如果您确实想JSP与 spring boot一起使用,这里有两个示例:

https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp

https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp

https://github.com/joakime/spring-boot-jsp-demo

https://github.com/joakime/spring-boot-jsp-demo

回答by dagnelies

To summarize it, noneof the suggested answers worked for me so far. Using a blank Spring boot starter project.

总而言之,到目前为止,没有一个建议的答案对我有用。使用一个空白的 Spring Boot 启动项目。

Somehow, something looks hardwired inside Spring or servlets so that JSP mustbe in /webapp(or a subfolder).Unlike default thymeleaf templates which are looked up in /resources/templates.

不知何故,某些东西在 Spring 或 servlet 中看起来是硬连线的,因此 JSP必须/webapp(或子文件夹)中。与在/resources/templates.

I tried all kind of changes, really a lot of different configurations, but wasn't able to modify that behavior. It just produced complexity and was unable to serve the JSPs anymore. So, bottom line, if you're using JSPs, just put them in /webapp. It also works by addding zeroconfiguration using a controller like:

我尝试了所有类型的更改,确实有很多不同的配置,但无法修改该行为。它只是产生了复杂性,无法再为 JSP 提供服务。所以,最重要的是,如果您使用 JSP,只需将它们放在/webapp. 它还可以通过使用控制器添加配置来工作,例如:

@GetMapping("/foo") public String serveFoo() { return "relative-path-inside-webapp/foo.jsp"; }

@GetMapping("/foo") public String serveFoo() { return "relative-path-inside-webapp/foo.jsp"; }

On another note, by default, the /webappfolder will also be hidden in the Spring Toolsuite, so you'll have to manually configure it as a "source folder".

另请注意,默认情况下,该/webapp文件夹也将隐藏在 Spring 工具套件中,因此您必须手动将其配置为“源文件夹”。