Spring MVC在服务器上运行时出现HTTP状态404错误。

huangapple 未分类评论43阅读模式
英文:

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

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
	xsi:schemaLocation=&quot;
		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">

	&lt;!-- Step 3: Add support for component scanning --&gt;
	&lt;context:component-scan base-package=&quot;com.luv2code.springdemo&quot; /&gt;

	&lt;!-- Step 4: Add support for conversion, formatting and validation support --&gt;
	&lt;mvc:annotation-driven/&gt;

	&lt;!-- Step 5: Define Spring MVC view resolver --&gt;
	&lt;bean
		class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
		&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot; /&gt;
		&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
	&lt;/bean&gt;

&lt;/beans&gt;

Here is myweb.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
	xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;
	id=&quot;WebApp_ID&quot; version=&quot;3.1&quot;&gt;

	&lt;display-name&gt;spring-mvc-demo&lt;/display-name&gt;

	&lt;absolute-ordering /&gt;

	&lt;!-- Spring MVC Configs --&gt;

	&lt;!-- Step 1: Configure Spring MVC Dispatcher Servlet --&gt;
	&lt;servlet&gt;
		&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
		&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
			&lt;param-value&gt;/WEB-INF/spring-mvc-demo-servlet.xml&lt;/param-value&gt;
		&lt;/init-param&gt;
		&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
	&lt;/servlet&gt;

	&lt;!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --&gt;
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;

	&lt;!--
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;/WEB-INF/view/main-menu.jsp&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;

	--&gt;


&lt;/web-app&gt;

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(&quot;/&quot;)
    public String showPage() {
        return &quot;main-menu&quot;;
    }
}

Here is my main-menu.jsp file

&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
         pageEncoding=&quot;ISO-8859-1&quot; isELIgnored=&quot;false&quot; %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;body&gt;

&lt;H2&gt;Spring MVC Demo - Home Page&lt;/H2&gt;

&lt;/body&gt;

&lt;/html&gt;

Here is an image of my project structure:

Project Structure

Here is a picture of how I have my Tomcat server setup:

Run Configuration

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/

huangapple
  • 本文由 发表于 2020年4月5日 04:11:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61034167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定