本文介绍了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.

我需要重新配置以实际使用resourcestemplates文件夹内的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项目).我只是将apply plugin: 'war'添加到build.gradle,添加/更改了以下文件,使用gradle war构建了这个项目,并将其部署到了我的应用程序服务器(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:

index.jsp:

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

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

08-24 11:17