模板的资源模板文件夹而不是

模板的资源模板文件夹而不是

本文介绍了Spring Boot 使用带有 JSP 模板的资源模板文件夹而不是 webapp 文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启动了一个 Spring Boot MVC 项目,发现 resources 中有两个文件夹.一种称为templates,另一种称为static.我真的很喜欢这个文件夹设置.

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

问题是我的视图使用 JSP 模板.我无法将 .jsp 模板放在 templates 文件夹中并让它工作.我需要做的是在与 srcresources 相同的级别上创建一个 webapp 文件夹.将我的 JSP 模板放在那里,然后可以找到我的视图.

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

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

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

推荐答案

根据 Maven 文档 src/main/resources 将在 WAR 中的 WEB-INF/classes 结束.

According to the Maven documentation src/main/resources will end up in WEB-INF/classes in the WAR.

这对 application.properties 中的 Spring Boot 有用:

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

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

如果您更喜欢 Java 配置,这是一种方法:

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

@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;
    }
}

更新一个完整的例子

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

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 war and deployed it to my application server (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:见上

ApplicationConfiguration.java: see above

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:

DemoController.java:

@Controller
public class DemoController {

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

index.jsp:

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

这篇关于Spring Boot 使用带有 JSP 模板的资源模板文件夹而不是 webapp 文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:17