英文:
Spring MVC HTTP Status 404 error when running on Server
问题
以下是你提供的内容的翻译:
我一直在跟随在线教程学习 Spring MVC 的基础知识,并且在尝试在本地的 Tomcat 服务器上运行项目时遇到了一些问题。我已经阅读了多个相关讨论,但在当前的答案中找不到解决方案,所以我才提出了问题。我正在跟随的教程使用的是 Eclipse,而我使用的是 IntelliJ,因此在整体项目设置方面可能存在错误。
每当我尝试运行项目时,我都会收到 HTTP 状态未找到 - 404 错误。通过在我的 XML 文件中的 welcome-file-list 中包含它,我成功地使我的主菜单页面加载了,但我希望能够扫描我的控制器,以确定要从哪个 JSP 文件中获取内容。以下是我的代码:
这是我的 spring-mvc-demo-servlet.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 第三步:添加组件扫描的支持 -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- 第四步:添加转换、格式化和验证的支持 -->
<mvc:annotation-driven/>
<!-- 第五步:定义 Spring MVC 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC 配置 -->
<!-- 第一步:配置 Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 第二步:设置 Spring MVC Dispatcher Servlet 的 URL 映射 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<welcome-file-list>
<welcome-file>/WEB-INF/view/main-menu.jsp</welcome-file>
</welcome-file-list>
-->
</web-app>
这是我的 HomeController.java 文件:
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
这是我的 main-menu.jsp 文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<body>
<H2>Spring MVC Demo - 首页</H2>
</body>
</html>
这是我的项目结构的图片:
这是我配置 Tomcat 服务器的图片:
非常感谢大家提前的帮助。我真的很感激对此的帮助。我意识到这可能是一个简单的答案,但我无法理解它。
英文:
I have been following an online tutorial in order to learn the basics of Spring MVC, and am running into some issues when I attempt to run the project on a local Tomcat server. I have read multiple discussions on here already, but cannot find the solution amongst the current answers which is why I am asking. The tutorial I am following is utilizing Eclipse, whereas I am using intellij, there there could have been an error on my part in the overall project set up as well.
Whenever I attempt to run my project I get a HTTP Status not found - 404 error. I was able to successfully get my main-menu page to load by including it in welcome-file-list in my xml file, but I want my controllers to be scanned in order to determine which jsp file to pull from. Below is my code:
Here is my spring-mvc-demo-servlet.xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Here is myweb.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<welcome-file-list>
<welcome-file>/WEB-INF/view/main-menu.jsp</welcome-file>
</welcome-file-list>
-->
</web-app>
Here is my HomeController.java file
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
Here is my main-menu.jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<body>
<H2>Spring MVC Demo - Home Page</H2>
</body>
</html>
Here is an image of my project structure:
Here is a picture of how I have my Tomcat server setup:
Thank you all in advance. I really appreciate the help with this. I realize it is probably a simple answer, but I cannot wrap my head around it.
答案1
得分: 0
不要运行HomeController.java文件。
点击spring-mvc-demo进行运行
不要尝试直接运行JSP文件。这不起作用是因为MVC。
您需要访问正确的URL,即localhost:8080/spring-mvc-demo/。
英文:
Don't run HomeController.java file.
Run as click on spring-mvc-demo
Do not attempt to run the JSP files directly. This will not work due to MVC.
You need to access the correct URL, localhost:8080/spring-mvc-demo/
专注分享java语言的经验与见解,让所有开发者获益!
评论