spring 无法使用 javaconfig 解析名称为“dispatcher”的 servlet 中名称为“htmlviews/index.html”的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37310864/
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
Could not resolve view with name 'htmlviews/index.html' in servlet with name 'dispatcher' using javaconfig
提问by Edgar
I get such exception:
我得到这样的例外:
javax.servlet.ServletException: Could not resolve view with name 'htmlviews/index.html' in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1211)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
when I try to connect to fully java configured spring web service.
当我尝试连接到完全 Java 配置的 Spring Web 服务时。
My configuration classes:
我的配置类:
@Configuration
@EnableWebMvc
@ComponentScan({"config", "controller"})
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/htmlviews/**").addResourceLocations("/htmlviews/");
}
}
Initializer:
初始化程序:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{MyWebConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return null;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
and controller:
和控制器:
@Controller
public class IndexController {
@RequestMapping(value = "/")
public String getIndexPage() {
return "htmlviews/index.html";
}
@RequestMapping(value = "/{[path:[^\.]*}")
public String index() {
return "forward:/";
}
}
whole file srtucture is simple :
整个文件结构很简单:
I am using Idea IDE (also tried in eclipse, same exception) and trying to deploy on tomcat. In pom.xml
, I added 'jstl' dependency, but that did not help to resolve problem.
Using xml
configuration everything works well. I have no idea what is wrong with my spring java configuration, it is super simple, maybe I forgot something?
我正在使用 Idea IDE(也在 eclipse 中尝试过,同样的异常)并尝试在 tomcat 上部署。在 中pom.xml
,我添加了“jstl”依赖项,但这无助于解决问题。使用xml
配置一切正常。我不知道我的spring java配置有什么问题,它超级简单,也许我忘记了什么?
Fixed itEverything started working when I changed spring version from 4.1.0.RELEASE
to 4.2.3.RELEASE
. I do not why it does not work with 4.1.0.RELEASE
. Maybe someone can explain, just curious.
修复它当我将 spring 版本从 更改为 时,一切都开始工作4.1.0.RELEASE
了4.2.3.RELEASE
。我不知道为什么它不起作用4.1.0.RELEASE
。也许有人可以解释一下,只是好奇。
回答by Minjun Yu
Problem
问题
Spring is trying to find views under your webappdirectory. Since you do not have any view resolver, Spring cannot resolve "htmlviews/index.html". In other words, Spring does not know what it is.
You have a Resource Resolverfor your html page, which is OK because HTML is static.
Spring 正在尝试在您的webapp目录下查找视图。由于您没有任何视图解析器,Spring 无法解析“htmlviews/index.html”。换句话说,Spring 不知道它是什么。
您的 html 页面有一个资源解析器,这没问题,因为 HTML 是静态的。
Possible Solution 1
可能的解决方案 1
In your MyWebConfigclass, add the following:
在您的MyWebConfig类中,添加以下内容:
@Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.jsp("/htmlviews/", ".jsp");
}
OR you can do this:
或者你可以这样做:
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/htmlviews/");
resolver.setSuffix(".jsp");
return resolver;
}
Change your html page to jsp page, I recommend that because jsp is simply more powerful than HTML.
将您的 html 页面更改为 jsp 页面,我建议这样做,因为 jsp 只是比 HTML 更强大。
Possible Solution 2
可能的解决方案 2
Pult all your htmlviewsfolder under resources so that Spring can find it according to your Resource Resolver.
把你所有的htmlviews文件夹放到资源下,这样 Spring 就可以根据你的资源解析器找到它。
Update
更新
It's rarely the case that HTML is needed in a Spring boot app. I highly recommend using a template engine (Thymeleafis preferred). This way, the sensible default setup is sufficient for most of the multi-page applications.
在 Spring 启动应用程序中很少需要 HTML。我强烈建议使用模板引擎(首选Thymeleaf)。这样,对于大多数多页应用程序来说,明智的默认设置就足够了。
回答by Vikash
I was trying to implement demo https://spring.io/guides/gs/securing-web/but i was facing similar problem, To note- this demo only have html with thymleaf (no JSP) and I missed to add thymleaf dependency(reason of error) earlier it showed error
我试图实现演示https://spring.io/guides/gs/securing-web/但我遇到了类似的问题,请注意-这个演示只有带有 thymleaf 的 html(没有 JSP),我错过了添加 thymleaf 依赖项(错误原因)早些时候它显示错误
Circular view path []: would dispatch back to the current handler URL ..error
Then I added bean view resolver and it started to give error .
然后我添加了 bean 视图解析器,它开始给出错误。
Could not resolve view with name..error
Finally it worked after removing the bean view resolver and adding dependency for thymleaf. Adding this made my project work.
最后它在删除 bean 视图解析器并为 thymleaf 添加依赖项后工作。添加这个使我的项目工作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Some more help I found to understand all this working at How to avoid the "Circular view path" exception with Spring MVC test
我在如何使用 Spring MVC 测试避免“圆形视图路径”异常中找到了更多帮助来理解所有这些工作