【发布时间】:2020-01-26 05:56:36
【问题描述】:
我想提供静态内容,例如 host:8080/ 从 /static 文件夹返回 index.html 和其他 html 文件,同时允许来自静态 signup.html 页面的 POST 生成呈现的 .jsp 视图.
开箱即用,SpringBoot 使用默认的 ResourceResolver 为我的静态资源提供服务,但重定向和转发到我的 .jsp 资源会导致未渲染的 JSP。
这是 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
如何在保持静态文件夹的同时将我的 .jsp 文件通过渲染?这是可能的还是我应该将我的网络内容移到网络服务器上?
这与Forward to JSP within Spring Controller after form submission有关
这个问题似乎已经解决了 2010 年使用 web 描述符和其他 XML 配置文件的问题 - How to handle static content in Spring MVC?
【问题讨论】:
-
根据您上面的配置,您的静态资源路径将类似于“/resources/public/template/xyz.jsp”,但正如您提到的,您将文件保存在“resources/templates/xyz.jsp”下。 jsp”。我认为修复资源位置会起作用。
标签: java spring spring-boot spring-mvc jsp