我正在努力解决这里描述的问题
No mapping found for HTTP request with URI Spring MVC
问题是,一旦资源解析器解析了视图,它就会再次重定向到Dispatcher,而Dispatcher没有找到映射到它的方法。
例如,如果用户请求/test
并且解析器将其映射到/views/test.jsp
,则调度程序而不是呈现响应会尝试再次查找路径/views/test.jsp
。
我试图将默认调度程序servlet路径更改为/
(默认情况下,它是/
)。
它不适用于Spring Boot配置。
我想知道是否有解决方案,而无需将调度程序路径设置为/
以外的其他内容,如/page
。
以下是产生问题的测试代码。 (这里是测试项目的{github链接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
以下是相关日志
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
答案 0 :(得分:1)
我终于找到了解决方案。
包含以下依赖项可解决此问题。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
我猜测内部存在JSP解析错误(或者JSTLView不存在),它将查看请求重定向到调度程序。
包含这些依赖项后,JSP视图成功呈现。
答案 1 :(得分:0)
对于那些没有运气尝试其他解决方案的人,您可能需要检查Controller类中是否存在ResponseBody批注。
@Controller
@EnableWebMvc
public class MainController {
@Autowired
BO bo = new BO();
@RequestMapping(value = "/sayHello",
method = RequestMethod.GET,
produces = "application/json")
public @ResponseBody String sayHello() {
return "Hellaa!!";
}
}