Spring MVC with Boot:“出现意外错误(type = Not Found,status = 404)” [英] Spring MVC with Boot: "There was an unexpected error (type=Not Found, status=404)"
问题描述
所以我是Spring的新手,到目前为止我已经运行了一个连接到MongoDB数据库的简单Web API,但是我在使用.jsp或.html文件生成普通的旧视图时遇到了麻烦。我尝试了各种不同的方法: InternalResourceViewResolver
, XmlViewResolver
,返回字符串而不是 ModelAndView
对象,似乎没有什么对我有用。我有以下代码:
So I'm new to Spring and I've so far gotten a simple web API running connected to a MongoDB database, but I'm having trouble generating just plain old views using .jsp or .html files. I've tried a variety of different approaches: InternalResourceViewResolver
, XmlViewResolver
, returning Strings instead of ModelAndView
objects, nothing seems to be working for me. I have the following code:
编辑:这是我的项目的git repo: https://github.com/jwallp/Spring-Test
Edit: here is a git repo with my project: https://github.com/jwallp/Spring-Test
作为上述项目是的,我在转到 / index
时收到白标错误,其中说:
As the above project is, I am getting a white label error upon going to /index
which says:
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
编辑:所以我设法通过使用 spring.view.prefix
和 spring.view来最终加载视图。后缀
而不是 spring.mvc.view.prefix
等等,并移动我的 WEB-INF
从我的项目根目录到内部< project root> / src / main / webapp /
的目录。我只是想知道,如果我的 WEB-INF
目录包含在另一个目录中,它是否仍然按预期运行(使其内容不直接可见)?
Edit: So I managed to get the view to finally load by using spring.view.prefix
and spring.view.suffix
instead of spring.mvc.view.prefix
and such, and by moving my WEB-INF
directory from my project root to inside <project root>/src/main/webapp/
. I just wanted to know, if my WEB-INF
directory is contained within another directory, will it still function as intended (making its contents not directly visible)?
推荐答案
希望你的类路径中有JSP库。如果你正在使用maven,包括 pom.xml
中的以下依赖项将包含:
Hope you have the JSP libraries in your classpath. If you are using maven, including the following dependencies in pom.xml
will have those:
<!-- For using JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- If you want to use JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
此外,您可能需要将此行放在JSP文件的顶部:
Also, you may need to put this line at the top of the JSP file:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
更新
在我的环境中对GitHub项目进行以下更改:
Doing the following changes to your project at GitHub worked in my environment:
- 将WEB-INF文件夹移动到
的src /主/ web应用
。这就是它的地方。 -
在
application.properties
替换
- Move the WEB-INF folder into
src/main/webapp
. That's the place for it. In
application.properties
replace
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
with
spring.view.prefix: /WEB-INF/pages/
spring.view.suffix: .jsp
似乎前者将使用Spring Boot 1.3 ,但不是目前稳定发布。
Seems the former will work with Spring Boot 1.3, but not not with the current stable release.
这篇关于Spring MVC with Boot:“出现意外错误(type = Not Found,status = 404)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!