【问题标题】:Getting 404 error when making a request from Spring MVC从 Spring MVC 发出请求时出现 404 错误
【发布时间】:2015-03-18 02:43:57
【问题描述】:

我创建了一个 war 文件并将其保存在 tomcat 的 webapps 目录中并启动 tomcat,但是当我点击 http://localhost:8181/HelloWeb/hello 时,我收到 404 错误。我是 spring mvc 的新手,任何帮助将不胜感激。以下是我的文件(执行http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm 中给出的示例。

HelloController.java

package dummyPackageOne;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloController {



@RequestMapping(method=RequestMethod.GET)
public String printHello(ModelMap modelMap){

    modelMap.addAttribute("message", "this is the message");
    return "hello";
    }
}

这是我的 hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

HelloWeb-Servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="dummyPackageOne" />
  <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <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://java.sun.com/xml/ns/javaee"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springTest</display-name>
  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>  org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>

【问题讨论】:

  • 您的 .war 文件的名称是什么?是 HelloWeb.war、ROOT.war 还是别的什么?
  • 另外,你能看到你的 .war 文件正在被 Tomcat 解压吗(也在 webapp 文件夹中)?
  • springTest.war 并以名称 springTest 解压。在 WEB-INF 文件夹中,我有 HelloWeb-servlet.xml、web.xml、classes/HelloController.java、jsp/hello.jsp、lib/*jars(所有 jar 文件)。
  • 检查了战争名称 HelloWeb.war 。但同样的问题。
  • tomcat 在 8181 上运行?

标签: java spring-mvc


【解决方案1】:

尝试在&lt;context:component-scan base-package="dummyPackageOne" /&gt; 之后添加&lt;mvc:annotation-driven&gt;。这是注册 HandlerMapping 和 HandlerAdapter 所必需的。

还要确保您的战争/爆炸战争名称与您的网络上下文相匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多