Half_Duplex
Half_Duplex

Reputation: 5234

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?

Upvotes: 0

Views: 883

Answers (1)

Alien
Alien

Reputation: 15908

You have a typo here.

Try changing to below line by changing template to templates.

resolver.setPrefix("/templates/")

Upvotes: 1

Related Questions