Spring-mvc组件扫描和视图控制器一起工作

时间:2012-11-24 18:27:20

标签: spring-mvc

以下是我的 application-context.xml

<context:component-scan base-package="org.godfather"></context:component-scan>
<mvc:view-controller path="/main.do" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/WEB-INF/" />
      <property name="suffix" value=".jsp" />
</bean>

问题: 如果我发表评论<mvc:view-controller path="/main.do" />,那么http://localhost:8181/tiles-app-1.1.1/header.do网址正在运行,但如果没有注释掉,那么该网址无效,获取No mapping found for HTTP request with URI [/tiles-app-1.1.1/header.do] in DispatcherServlet with name 'mytiles'为什么会这样?

Web.xml中

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/application-context.xml
    </param-value>
</context-param>  

<servlet>
    <servlet-name>mytiles</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>mytiles</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

控制器

package org.godfather;
@Controller
public class AppointmentController {

@RequestMapping("/header")
public void get() {
    System.out.println("get()");
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您将相同的Spring Bean定义文件(/WEB-INF/application-context.xml)传递给ContextLoaderListenerDispatcherServlet

DispatcherServlet应该有自己的Spring Bean定义文件

this question。所有Spring MVC控制器和特定的MVC xml都应该在不同的文件中。