[英]Tomcat returns a 404 page using Spring MVC; controller not reached
I have a controller and a JSP. 我有一个控制器和一个JSP。 That is all. 就这些。 I export my project as a .war
and put it in the tomcat webapps
directory running on Linux mint. 我将项目导出为.war
,并将其放在Linux mint上运行的tomcat webapps
目录中。 When I type localhost:8080/filename
into the address bar, it returns the Tomcat 404 message. 当我在地址栏中键入localhost:8080/filename
,它将返回Tomcat 404消息。
Controller: 控制器:
package web.controllers;
import java.util.Random;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/")
public class HomeController
{
@RequestMapping(method = RequestMethod.GET)
public String displayHomePage(Model model)
{
model.addAttribute("display",new Random().nextInt());
return "/index";
}
}
index.jsp
: index.jsp
:
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>Random int: ${display}</p>
</body>
</html>
The JSP file is in the WEB-INF
folder under WebContent
(the default for the Eclipse dynamic webapps project setup). 该JSP文件位于WebContent
(Eclipse动态webapps项目设置的默认值)下的WEB-INF
文件夹中。 All Spring jars are referenced for project and there are no compile errors. 所有Spring jar都引用了该项目,并且没有编译错误。 What am I missing here? 我在这里想念什么?
Have you configured a view resolver ? 您是否配置了视图解析器 ? That would infers a view, like your JSP file, from your return value eg "index". 这将根据您的返回值(例如“ index”)推断出一个视图,例如您的JSP文件。
If you are using Spring Boot, just putting a couple of lines in application.properties does that. 如果您使用的是Spring Boot,只需在application.properties中添加几行即可。 For example, in one of my projects, I put my JSPs inside WEB-INF/jsp
folder, and have these couple of lines in my application.properties : 例如,在我的一个项目中,我将JSP放在WEB-INF/jsp
文件夹中,并将这两行放在我的application.properties中 :
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
Then returning "index"
(no beginning slash) would get WEB-INF/jsp/index.html
. 然后返回"index"
(不加斜杠)将得到WEB-INF/jsp/index.html
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.