【问题标题】:Serve html files in Spring and Thymeleaf without Request Mapping在没有请求映射的情况下在 Spring 和 Thymeleaf 中提供 html 文件
【发布时间】:2015-02-28 09:10:32
【问题描述】:

我的地图有问题。我有一个具有以下配置的模板解析器:

    @Bean(name="templateResolver")
    public ServletContextTemplateResolver getServletContextTemplateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    @Bean(name="templateEngine")
    public SpringTemplateEngine getTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(getServletContextTemplateResolver());
        return templateEngine;
    }

    @Bean(name="viewResolver")
    public ThymeleafViewResolver getThymeleafViewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(getTemplateEngine());
        viewResolver.setOrder(1);
        viewResolver.setCharacterEncoding("UTF-8");
        return viewResolver;
    }

我有我的项目结构:

-WEB-INF
--views
---home.html
---registration.html

我正在使用 thymeleaf 和 spring 4.0。对于家庭,我在控制器中添加了以下 requestMapping:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String start(Model model) {
    return "home";
}

我没有registration.html的请求映射。

在我的 home.html 中,我有以下链接(百里香锚标记):

        <a href="registration.html" th:href="@{/registration.html}" th:text="#{navigation.registration}">Registration</a>

我已经在 /myapp 下的 tomcat 中绑定了我的应用程序,当我进入 /myapp 时,我得到了 home.html 页面(我有一个请求映射),但是当我进入 /myapp/registration.html 时,我得到了404.

我必须做什么,在不将每个页面都添加到控制器的情况下提供这样的静态 html 文件?可能吗?我尝试使用静态资源,但它不起作用。

我还尝试将视图目录向上移动,这样我就有了结构:

-WEB-INF
-views
--home.html
--registration.html

我可以进入 /myapp/views/registration.html,但我无法进入 /myapp/registration.html

【问题讨论】:

    标签: java html spring thymeleaf static-html


    【解决方案1】:

    要在没有控制器的情况下访问页面(就像 Layout 一样),请使用 th:substituteby="path/to/registration" 。并尝试它。它将根据请求呈现registration.html。

    【讨论】:

    • 什么意思?这个参数被添加到一个 div 或 body 中,我想去完全新的页面。我不想留在主页上,并交换内容。你能解释一下你的意思吗?
    • 我的意思是,在首页本身我们可以显示registration.html。如果您想在不同的页面中显示 registeration.html,那么您必须在客户端-服务器架构中工作时提供服务器请求。否则在 home.html 中显示您的 registration.html 并使用 css 或 js 隐藏 home.html 内容。
    【解决方案2】:

    您打算在 myapp 的根目录上获取registration.html 吗?您的解决方案是映射这些请求的正常行为。如果您想提供静态内容,您可以使用以下操作,例如:
    使用 xml:

    <mvc:resources mapping="/views/**" location="/views/" />
    

    在您的包中带有注释和附加类:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/views/**").addResourceLocations("/views/");
        }
    }
    

    然后你也可以使用你提到的控制器类。静态页面可在 /views/yourpage.html 下访问。这是与 /views/registration.html 类似的解决方案,但在 spring 中提供静态资源的方式更方便。

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2018-09-03
      • 2018-01-02
      • 2014-06-06
      • 1970-01-01
      • 2022-01-04
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多