繁体   English   中英

使用 Spring MVC 提供 static 内容并渲染 JSP

[英]Serving static content and rendering JSP with Spring MVC

I would like to serve static content, such that host:8080/ returns index.html and other html files from a /static folder while allowing a POST from a static signup.html page to result in a rendered.jsp view.

开箱即用,SpringBoot 使用默认的 ResourceResolver 为我的 static 资源提供服务,但重定向并转发到 my.jsp 资源会导致未渲染的 Z2D7FCA4F5778E4B29B60691471。

这是 MVC 配置

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/**")
          .addResourceLocations("/resources/public");


    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/template/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }

}

这些文件根据上面的代码定位:

src/main/resources/static
src/main/resources/templates

如何在维护 static 文件夹的同时将 my.jsp 文件通过渲染? 这是可能的还是我应该将我的 web 内容移出 web 服务器?

这与在表单提交后转发到 Spring Controller 内的 JSP 有关

This question seems to have solved the problem in 2010 using web descriptor and other XML config files - How to handle static content in Spring MVC?

你这里有一个错字。

尝试通过将模板更改为模板来更改为下一行。

resolver.setPrefix("/templates/")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM