I'm struggling with the issue described here
No mapping found for HTTP request with URI Spring MVC
Problem is once the resource resolver resolves the view, it's again redirected to the Dispatcher, which doesn't find the a method mapped to it.
For example if user requests for /test
and resolver maps it to /views/test.jsp
, dispatcher instead of rendering the response tries to lookup path /views/test.jsp
again.
I have tried to change the default dispatcher servlet path to /
(which by the way is /
by default).
It doesn't work with Spring Boot configuration.
I would like to know if there's a solution for this without having to set dispatcher path to something other than /
like /page
.
Following is test code to produce the issue. (Here's github link for the Test project https://github.com/ConsciousObserver/stackoverflow/tree/master/TestMvc)
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class TestMvcApplication {
public static void main(String[] args) {
SpringApplication.run(TestMvcApplication.class, args);
}
}
@Configuration
class MvcConfig extends WebMvcConfigurerAdapter {
public MvcConfig () {
System.out.println("%%%%%%%%%%%%% " + getClass() + " loaded %%%%%%%%%%%%%%%");
}
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet);
System.out.println("%%%%%%%%%%%%%%%%% Adding dispatcher servletmapping");
registration.addUrlMappings("/");
return registration;
}
}
@Controller
class TestController {
@RequestMapping("test")
public String test() {
return "test";
}
}
application.properties
logging.level.org.springframework.web=DEBUG
spring.mvc.view.prefix: /views/
spring.mvc.view.suffix: .jsp
Following are the relevant logs
Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] based on requested media type 'text/html'
Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet'
Forwarding to resource [/views/test.jsp] in InternalResourceView 'test'
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/views/test.jsp]
Looking up handler method for path /views/test.jsp
Did not find handler method for [/views/test.jsp]
Matching patterns for request [/views/test.jsp] are [/**]
URI Template variables for request [/views/test.jsp] are {}
Mapping [/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@21362712]]] and 1 interceptor
Last-Modified value for [/views/test.jsp] is: -1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request
Successfully completed request