spring mvc中的根映射

时间:2014-09-25 10:35:29

标签: spring spring-mvc

我使用maven创建了一个spring MVC项目并在eclipse中导入它。并将“/”映射到controller.But当我运行porjectit时给出错误404警告:在DispatcherServlet中找不到带有URI [/ MyProject /]的HTTP请求的映射,名称为'mvc-dispatcher'

我的配置是:  的web.xml

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
                   org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
                  org.springframework.web.context.ContextLoaderListener
            </listener-class>
</listener> 

HelloController.java:

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

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

}

和mvc-dispatcher-servlet.xml:

<context:component-scan base-package="com.mkyong.common.controller" />
<context:annotation-config></context:annotation-config>
<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>

现在,如果我将requestmapping更改为“/ welcome”,然后使用/ welcome调用url,则可以正常工作。那么为什么只有root这个不起作用

0 个答案:

没有答案