从Spring MVC发出请求时出现404错误

时间:2015-03-18 02:43:57

标签: java spring-mvc

我创建了一个war文件并将其保存在tomcat的webapps目录中并启动tomcat但是当我点击 http://localhost:8181/HelloWeb/hello 时,我收到404错误。我是春天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>

1 个答案:

答案 0 :(得分:0)

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

同时确保您的战争/爆炸战争名称与您的网络背景相匹配。

相关问题