使用Spring Boot在嵌入式Tomcat服务器上部署资源

霍尔监狱

我有一个项目,其中将来自多个来源的数据处理为某些数据结构。在程序完成这些结构的构建之后,我希望它设置一个服务器,使用户可以手动微调这些结构。我认为使用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,服务器对URL的回复http://localhost:8080/greeting也是

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似乎配置不正确。经过一番摆弄之后,我最终将spring-boot-starter-parentin的版本pom.xml更改0.5.0.BUILD-SNAPSHOT0.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才能从中删除version标记thymeleaf-spring3,为什么这样做是必需的?

戴夫·赛尔(Dave Syer)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot服务器无法启动嵌入式Tomcat服务器

如何将简单的Spring Boot(带有Gradle构建系统)部署到Apache Tomcat(真实服务器,而不是嵌入式服务器)?

在Linux的嵌入式Tomcat服务器中运行Spring Boot应用程序

Spring Boot是否以战争方式部署在外部服务器上时使用外部服务器还是嵌入式服务器?

如何关闭Spring Boot嵌入式服务器

在大型企业中运行Spring Boot Microservices是否需要嵌入式Tomcat服务器的许可证?

嵌入式Tomcat强化-如何在Spring Boot中更改/覆盖广告服务器信息?

无法启动 bean 'webServerStartStop';无法启动嵌入式 Tomcat 服务器 - spring-boot-starter-web

Spring Boot为嵌入式服务器配置自定义jsessionid

具有不同嵌入式服务器的Spring Boot WAR大小

spring boot中嵌入式solr服务器的类路径是什么?

使用嵌入式 tomcat 更改服务器标头

在 tomcat 外部服务器上部署 spring boot restfull api 时出错

使用IntelliJ部署支持嵌入式tomcat的spring-boot应用程序

使用Spring嵌入式tomcat(.jar)公开资源

使用FastCGI的嵌入式Web服务器

Spring Boot嵌入式tomcat日志

Spring Boot嵌入式Tomcat性能

将Spring Boot Weblfux与嵌入式Tomcat结合使用

有没有办法用spring boot 1.x选择嵌入式服务器类型

如何创建/查找应用程序jar以使用嵌入式tomcat部署Spring Boot Maven应用程序?

Nginx Plus无法使用Docker嵌入式DNS服务器解析服务

Spring Boot-替换默认的嵌入式Tomcat连接器

在Spring Boot中区分嵌入式Tomcat中多个连接器/端口之间的请求

Jetty嵌入式服务器和Spring Security集成

SolrJ嵌入式服务器Spring Config不再起作用

Spring Boot:WebServerException:无法启动嵌入式Tomcat

修改Spring Boot嵌入式Tomcat提取路径

Spring Boot,无法初始化嵌入式tomcat