Spring Hello world不工作-404错误

时间:2013-12-09 06:52:08

标签: java spring spring-mvc

我使用spring尝试了简单的Hello world程序。它没有按预期工作。

package com.Spring;

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
public class Hello {

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String helloWorld(ModelMap model){
        model.addAttribute("message", "hello world");
        return "hello";

    }

}

HelloWeb-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-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-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.Spring" />

    <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">
  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>com.Spring.Hello</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
    </context-param>
        <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

hello.jsp文件。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${message}
</body>
</html>

我已经使用ant和deploy创建了war文件。部署war文件后终端没有错误。现在我尝试访问浏览器中的URL,显示404错误。

http://ipaddress:8080/SpringHello/hello.jsp

如何访问网址?或者我做错了什么?

2 个答案:

答案 0 :(得分:1)

上下文文件中也缺少Handlermapping。因此,在Kamlesh建议的基础上添加注释驱动的bean。

<mvc:annotation-driven />

答案 1 :(得分:0)

您在web.xml中添加了错误的servlet类。

<servlet-class>com.Spring.Hello</servlet-class>

应该是:

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

更多了解:http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/