Java 使用 Spring Boot 配置 ViewResolver 和注解给出了没有为带有 URI 错误的 HTTP 请求找到映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29953245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error
提问by otognan
I'm trying to make "hello world" application with gradle, spring boot and spring mvc with the simplest view resolver and html.
我正在尝试使用最简单的视图解析器和 html 使用 gradle、spring boot 和 spring mvc 制作“hello world”应用程序。
I started from the thymeleaf spring boot exampleand I just wanted to remove thymeleaf to make a simpler mvc application using pure html and InternalResourceViewResolver. I have a single greeting.html I want to serve which is located at src/main/webapp/WEB-INF. When I run the app I get
我从thymeleaf spring boot 示例开始,我只是想删除 thymeleaf 以使用纯 html 和 InternalResourceViewResolver 制作一个更简单的 mvc 应用程序。我有一个我想要提供的 greeting.html,它位于 src/main/webapp/WEB-INF。当我运行应用程序时,我得到
No mapping found for HTTP request with URI [/WEB-INF/greeting.html] in DispatcherServlet with name 'dispatcherServlet'
This is a common error and there are a lot of answers on the web but nothing seems to help.
这是一个常见的错误,网上有很多答案,但似乎没有任何帮助。
Here is my Application.java
这是我的 Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Here is my GreetingController.java
这是我的 GreetingController.java
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting() {
return "greeting";
}
}
Here is my MvcConfiguration.java
这是我的 MvcConfiguration.java
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
}
}
I run it with gradle bootRun
我运行它 gradle bootRun
Here is the repo with the code: https://github.com/driver-pete/spring-mvc-example
这是带有代码的 repo:https: //github.com/driver-pete/spring-mvc-example
Here are some more clues:
这里还有一些线索:
- Thymeleaf view resolving works fine
- InternalResourceViewResolver resolves to the right path
- WEB-INF and greeting.html seems to be present in the war file
- I do not have jsp or jstl so I do not miss those jars as some might suggest
- Thymeleaf 视图解析工作正常
- InternalResourceViewResolver 解析到正确的路径
- WEB-INF 和 greeting.html 似乎存在于 war 文件中
- 我没有 jsp 或 jstl,所以我不会像有些人建议的那样错过那些罐子
My hypothesis is that dispatcher servlet somehow get configured to serve on /* instead of / like hereand everywhere. However I don't have web.xml so those advices do not apply here. I see a lot of examples how to configure dispatcher servlet programmatically but I want to keep my app at minimum and I suspect that spring boot is supposed to configure it ok since it works fine with thymeleaf.
我的假设是调度程序 servlet 以某种方式被配置为在 /* 而不是 / 像这里和任何地方一样提供服务。但是我没有 web.xml 所以这些建议在这里不适用。我看到了很多如何以编程方式配置调度程序 servlet 的示例,但我想将我的应用程序保持在最低限度,并且我怀疑 Spring Boot 应该可以对其进行配置,因为它可以与 thymeleaf 一起正常工作。
采纳答案by Biju Kunjummen
You only need to enable the default servlet, this is done by adding the following to your MvcConfiguration
:
您只需要启用默认 servlet,这是通过将以下内容添加到您的MvcConfiguration
:
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Essentially what is happening is Spring does not know how to handle the handling of such content natively(could be a jsp say), and to this configuration is the way to tell it to delegate it to the container.
本质上发生的事情是 Spring 不知道如何在本机处理此类内容(可能是 jsp 说),而这种配置是告诉它委派给容器的方式。
回答by otognan
After investigating more I discovered an alternative solution that works without adding configureDefaultServletHandling method. You need to add an embedded tomcat jsp engine to build.gradle:
在调查了更多之后,我发现了一种无需添加 configureDefaultServletHandling 方法即可工作的替代解决方案。需要在 build.gradle 中添加一个内嵌的 tomcat jsp 引擎:
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
As opposed to configureDefaultServletHandling method this solution works not only with plain html but also with jsp.
与 configureDefaultServletHandling 方法相反,该解决方案不仅适用于纯 html,还适用于 jsp。
All solutions are available at: https://github.com/driver-pete/spring-mvc-exampleThis solution is available on master. Biju's solution is on DefaultServletHandling_solution branch.
所有解决方案都可以在:https: //github.com/driver-pete/spring-mvc-example这个解决方案在 master 上可用。Biju 的解决方案在 DefaultServletHandling_solution 分支上。
回答by Piyush Upadhyay
View resolver can also be configured in application.properties
file of Spring-Boot web applications, something like below:
视图解析器也可以在application.properties
Spring-Boot Web 应用程序的文件中配置,如下所示:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp