Gabriel Bleu
Gabriel Bleu

Reputation: 10204

How to redirect an url ending by ".jsp"

I have a mapped URL to /foo.jsp which redirects to a controller, but it is not handled by spring and returns a 404. It seems that the URL is ignored and directly sent to tomcat. Is it possible to make spring handle it?

To reproduce, starting from https://github.com/spring-projects/spring-boot/tree/1.4.x/spring-boot-samples/spring-boot-sample-tomcat7-jsp just add:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/foo", "/").setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        registry.addRedirectViewController("/foo.html", "/").setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        registry.addRedirectViewController("/foo.jsp", "/").setStatusCode(HttpStatus.MOVED_PERMANENTLY);
    }
}

Then navigating to /foo and /foo.html works as expected, but /foo.jsp returns a 404

Upvotes: 1

Views: 388

Answers (1)

Akash Mishra
Akash Mishra

Reputation: 712

Would you use a viewResolver to handle the jsp views. And segregate html and jsp views differently using this technique.

Upvotes: 1

Related Questions