在Eclipse中运行spring MVC应用程序时出现HTTP-404错误 - 教程指向hello world示例

时间:2015-08-14 06:35:58

标签: java eclipse jsp spring-mvc tomcat

我是Spring的初学者我正在从教程中学习点,下面的代码与教程中的内容完全相同,但是当我尝试运行它时,我得到404错误,代码是hello world程序,当我运行时eclipse使用tomcat服务器我收到这样的错误

我在浏览器上使用的网址是http://localhost:8080/HelloWeb/hello

HelloController.java:

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

的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="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

的web.xml:

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</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>

的hello.jsp:

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

这是我的目录结构

HelloWeb项目:

|--HelloController.java
|-- WebContent
   `-- WEB-INF
         |-- jsp
              |-- web.xml

1 个答案:

答案 0 :(得分:1)

http://howtodoinjava.com/2015/02/05/spring-mvc-hello-world-example/#hello_world

按照上面的URL,它完全有效我已经尝试过并确保你在pom.xml中添加了spring的依赖项。 同样在您的webapp文件夹中也应该存在HelloWeb-servlet.xml。