Java Spring MVC with Boot:“出现意外错误(类型=未找到,状态=404)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31900411/
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
Spring MVC with Boot: "There was an unexpected error (type=Not Found, status=404)"
提问by rawa
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:
所以我是 Spring 的新手,到目前为止我已经获得了一个连接到 MongoDB 数据库的简单 Web API,但是我无法使用 .jsp 或 .html 文件生成普通的旧视图。我尝试了各种不同的方法:InternalResourceViewResolver
, XmlViewResolver
,返回字符串而不是ModelAndView
对象,似乎没有什么对我有用。我有以下代码:
Edit: here is a git repo with my project: https://github.com/jwallp/Spring-Test
编辑:这是我的项目的 git repo:https: //github.com/jwallp/Spring-Test
As the above project is, I am getting a white label error upon going to /index
which says:
正如上面的项目一样,我在去的时候收到一个白标错误/index
,上面写着:
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.)
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)?
编辑:所以我设法通过使用spring.view.prefix
andspring.view.suffix
而不是spring.mvc.view.prefix
诸如此类,并将我的WEB-INF
目录从我的项目根目录移动到 inside来最终加载视图<project root>/src/main/webapp/
。我只是想知道,如果我的WEB-INF
目录包含在另一个目录中,它是否仍能按预期运行(使其内容不直接可见)?
回答by Danny Varela
Try the Following:
尝试以下操作:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController
{
@RequestMapping("/")
public String index()
{
return "index";
}
}
回答by Sanjay
Hope you have the JSP libraries in your classpath. If you are using maven, including the following dependencies in pom.xml
will have those:
希望您的类路径中有 JSP 库。如果您使用的是 maven,则包含以下依赖项pom.xml
将具有这些:
<!-- 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>
Also, you may need to put this line at the top of the JSP file:
此外,您可能需要将此行放在 JSP 文件的顶部:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
Update
更新
Doing the following changes to your project at GitHub worked in my environment:
在我的环境中对 GitHub 上的项目进行以下更改:
- Move the WEB-INF folder into
src/main/webapp
. That's the place for it. In
application.properties
replacespring.mvc.view.prefix=/WEB-INF/pages/ spring.mvc.view.suffix=.jsp
with
spring.view.prefix: /WEB-INF/pages/ spring.view.suffix: .jsp
Seems the former will work with Spring Boot 1.3, but not not with the current stable release.
回答by mirmdasif
Spring Boot has limited support for JSP, because of its use of an embedded servlet container. From the Spring Boot reference documentation:
Spring Boot 对 JSP 的支持有限,因为它使用了嵌入式 servlet 容器。从Spring Boot 参考文档:
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat. Jetty does not currently work as an embedded container with JSPs.
在运行使用嵌入式 servlet 容器(并打包为可执行存档)的 Spring Boot 应用程序时,JSP 支持存在一些限制。
使用 Tomcat 时,如果您使用 war 打包,它应该可以工作,即可执行的 war 可以工作,并且还可以部署到标准容器(不限于,但包括 Tomcat)。由于 Tomcat 中的硬编码文件模式,可执行 jar 将无法工作。Jetty 目前不能作为带有 JSP 的嵌入式容器工作。
Hereis a basic example of using jsp in spring boot application.
这是在 Spring Boot 应用程序中使用 jsp 的基本示例。
回答by lilabnersgal
We ran into this problem at work while upgrading an older application to Spring Boot. What we did:
我们在将旧应用程序升级到 Spring Boot 时在工作中遇到了这个问题。我们做了什么:
The prefix and suffix mappings as given above for
application.properties
(though our prefix was just/WEB-INF/
).Moved our CSS, JavaScript, HTML files to a resources\static folder. We had subdirectories under that for each type.
Places where
window.open("somefile.jsp")
was used, was changed towindow.open("somevalue.do")
wheresomevalue
mapped to a@RequestMapping
value and thesetViewName
for theModelAndView
of that method mapped to the previous jsp. Where there was awindow.open("somefile.html")
we changed it to map towindow.open("includes/somefile.html")
where includes is a subdirectory in our resources/static tree.
上面给出的前缀和后缀映射
application.properties
(尽管我们的前缀只是/WEB-INF/
)。将我们的 CSS、JavaScript、HTML 文件移动到 resources\static 文件夹。我们在每个类型下都有子目录。
其中的地方
window.open("somefile.jsp")
使用,改变为window.open("somevalue.do")
其中somevalue
映射到一个@RequestMapping
值,setViewName
对于ModelAndView
该方法的映射到前面的JSP。那里有一个window.open("somefile.html")
我们改变它映射到window.open("includes/somefile.html")
其中包括在我们的资源/静态树的子目录。
回答by keshab pandey
I ran into this problem few times. It was because I put /WEB-INF/ in /src/main/java folder. Latest I created separate path for the INF file in /src/main/webapp and I was able to run my application correctly and displayed the text in the browser.
我几次遇到这个问题。这是因为我将 /WEB-INF/ 放在 /src/main/java 文件夹中。最新的我在 /src/main/webapp 中为 INF 文件创建了单独的路径,我能够正确运行我的应用程序并在浏览器中显示文本。
回答by Abdur Rahman
Following jsp files relocation can solve the problem. I solved my problem this way:
以下jsp文件重定位可以解决问题。我这样解决了我的问题:
Move the .jsp files to:
将 .jsp 文件移动到:
"src/main/resources/META-INF/resources/WEB-INF/jsp".
Make sure the application.properties file contains:
确保 application.properties 文件包含:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
Your controller class should be like:
你的控制器类应该是这样的:
@Controller
public class IndexController
{
@RequestMapping("/")
public String index()
{
return "index";
}
}
If you are using maven, include the following dependencies in pom.xml
如果您使用的是 maven,请在 pom.xml 中包含以下依赖项
<!-- 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>
It should then work.
然后它应该工作。
回答by Tomasz Dudkowski
I added one line to my application.properties file.
我在 application.properties 文件中添加了一行。
server.tomcat.additional-tld-skip-patterns=hk2-utils.jar,javax.annotation-api.jar,javax.inject.jar,hk2-api.jar,config-types.jar,hk2-core.jar,hk2-config.jar,tiger-types.jar,validation-api.jar,jboss-logging.jar,classmate.jar,hk2-locator.jar,javassist.jar,hk2-runlevel.jar,class-model.jar,asm-all-repackaged.jar
It should then run.
然后它应该运行。
回答by Subha Chandra
I have used mvn spring-boot:run
我用过了 mvn spring-boot:run
It's worked for me
它对我有用
回答by Harsha
Eventhough I have tried many ways to fix this issue, couldn't find perfect solution.
尽管我尝试了很多方法来解决这个问题,但找不到完美的解决方案。
If you are using Intellij IDEA : do not try to run Spring Boot application(with dynamic .jsp views) with the IDE's run ?? button.
如果您使用的是 Intellij IDEA:不要尝试在 IDE 的运行中运行 Spring Boot 应用程序(使用动态 .jsp 视图)?按钮。
$ cd {PROJECT_FOLDER}
$ ls //Make sure that you are in the same folder where pom.xml resides
and then run below command
$ mvn spring-boot:run
Now your application should be served at localhost:8080.
现在您的应用程序应该在 localhost:8080 上提供服务。