2016-11-07 92 views
1

我得到错误:HTTP Status 404
用的警告:以下警告:未找到与URI Spring框架的HTTP请求映射

WARNING: No mapping found for HTTP request with URI [/p2/] in DispatcherServlet with name 'HelloWeb' 

是我的文件:

HelloController.java

package pack1; 

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("/") 
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="pack1" /> 

    <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 language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
hello 
</body> 
</html> 
+0

你需要添加方法 “你好” 到您的HelloController –

+0

你如何打电话给你的控制器? – Jens

回答

1

添加@RequestMapping("/p2")的控制器,如下图所示:

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

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

我改为@RequestMapping(“/ p2”),但仍然给出相同的错误 –

相关问题