【问题标题】:Deployment of resources on embedded Tomcat server using Spring Boot使用 Spring Boot 在嵌入式 Tomcat 服务器上部署资源
【发布时间】:2014-01-21 21:52:29
【问题描述】:

我有一个项目,其中来自多个来源的数据被处理成一些数据结构。在程序完成构建这些结构后,我希望它设置一个服务器,使用户能够手动微调这些结构。我决定使用 Spring Boot 设置的嵌入式 Tomcat 服务器上的 Spring MVC 正是我所需要的。

我想使用 Thymeleaf 作为视图技术,因此这样做了

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main {

    public static void main(String... args) throws Exception {
        // Lots of initialization ...

        SpringApplication.run(Main.class, args);
    }

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/resources/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCacheable(false);
        return resolver;

    }

    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        viewResolver.setViewNames(new String[]{"*"});
        viewResolver.setCache(false);
        return viewResolver;
    }
}

@Controller
public class WebController {
    @RequestMapping(value="/greeting", method=RequestMethod.GET)
    public String greeting() {
        return "greeting";
    }
}

但是即使/resources/views/greeting.html处有视图文件,服务器对http://localhost:8080/greeting的URL的回复是

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers

在调试器中单步执行代码后,似乎在某个时候,ServletContext(应该将视图文件作为流返回)在临时文件夹中查找,例如

C:\Users\Michael\AppData\Local\Temp\tomcat-docbase.971027024999448548.8080

这是空的。

现在我知道我需要这样做

  1. 在服务器启动时将资源部署到临时文件夹

  2. 让服务器在资源已经存在的目录中运行

我的问题只是我不知道该怎么做,或者哪​​种方法最好。有人告诉我 1 是更好的智慧,但欢迎提出任何建议。

编辑

好的,我最终得到了一些似乎有效的东西。虽然 Joe 的回答确实帮助我顺利完成了工作,但我似乎也不得不以一种令我困惑的方式更改我的 Maven 配置。

将模板greeting.html放入/resources/templates/greeting.html并将resources添加到构建路径后,出现错误

javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/word/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

换句话说,Thymeleaf 似乎没有正确配置。经过一番摆弄,我最终将pom.xml中的spring-boot-starter-parent的版本从0.5.0.BUILD-SNAPSHOT更改为0.5.0.M6

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--<version>0.5.0.BUILD-SNAPSHOT</version>-->
    <version>0.5.0.M6</version>
</parent>

并从 Thymeleaf 依赖项中删除版本标签

<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring3</artifactId>
        <!--<version>${thymeleaf-spring3-version}</version>-->
    </dependency>
</dependencies>

在这之后,它起作用了。

谁能解释一下为什么我需要更改spring-boot-starter-parent 的版本才能从thymeleaf-spring3 中删除版本标签,以及为什么需要这样做?

【问题讨论】:

  • Spring Boot 在 Spring 3 中不起作用(很多),所以当 thymeleaf 发布他们的 spring4 包时,Boot 切换到使用这些包。工件 id 不同,这就是为什么它不适用于旧的。变化肯定是在 M6 之后,所以如果你想要最新最好的(可能包括 M7),你需要 thymeleaf spring4 依赖项。

标签: spring tomcat spring-mvc spring-boot embedded-tomcat-7


【解决方案1】:

servlet 上下文根不是嵌入式服务器中模板的最佳位置。有一种方法可以做到这一点,但如果我是你,我会顺其自然并使用类路径。如果您允许 Spring Boot 配置模板解析器(也推荐),那么默认情况下它将在 classpath:/templates 中查找。在 Boot 代码库中有几个使用 thymeleaf 的示例,因此如果您有不同的要求,应该很容易修改其中之一。

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2016-04-03
    • 2019-10-11
    • 2017-04-07
    • 2017-11-27
    • 2021-10-26
    相关资源
    最近更新 更多