简体   繁体   中英

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.

Out of the box, SpringBoot is serving my static resources using the default ResourceResolver, but redirects and forwards to my.jsp resources result in an unrendered JSP.

This is MVC config

@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);
    }

}

The files are located according to code above:

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

How am I able to put my.jsp files through to be rendered while maintaining a static folder? Is this possible or should I just move my web content out to web server?

This is related to Forward to JSP within Spring Controller after form submission

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?

You have a typo here.

Try changing to below line by changing template to templates.

resolver.setPrefix("/templates/")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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