2015-06-27 83 views
1

的web.xml无法Spring MVC中映射的DispatcherServlet页

<?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" 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>SpringMVC4</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 

    <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> 

    <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> 

的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.test" /> 

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

Controller1.java内部定义com.test

package com.test; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/met") 
public class Controller1 { 
    @RequestMapping(value = "/{name}", method = RequestMethod.GET) 
    public String getName(@PathVariable String name, ModelMap model){ 
     model.addAttribute("operation", name); 
     return "met"; 
    } 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String getDefault(ModelMap model){ 
     model.addAttribute("operation", "Please enter an operation"); 
     return "met"; 
    } 

} 

当我去到http://localhost:8080/SpringMVC4/met

我得到错误

No mapping found for HTTP request with URI [/SpringMVC4/met] in DispatcherServlet with name 'HelloWeb' 
+0

而当你尝试'http:// localhost:8080/SpringMVC4/met /'? –

+0

“war”文件的名称是什么? – Jens

+0

@Jens:SpringMVC4.war – user544079

回答

1

请尝试添加< MVC:注解驱动你的HelloWeb-servlet.xml中/>。 也许它会有帮助。

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

<context:component-scan base-package="com.test" /> 

<mvc:annotation-driven /> 

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

</beans>