0

我曾经使用过 Spring 注释,但从未使用过 Spring xml。我必须创建一个应用程序,其中所有 bean 都保存在 applciationContext.xml 文件中,而不是 xxx-dispather.xml 中。但是,当我将 bean 放入 xxx-dispatcher.xml 中时,我可以访问控制器类并且一切正常,但是当我将相同的 bean 放入 applciationContext.xml 中时,它根本不起作用。我得到的只是 404 异常。

下面是我的 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 Web MVC Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

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

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

下面是我的 mvc-dispatcher-servlet.xml

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

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="caseSensitive" value="true" />
    <property name="pathPrefix" value="/customer" />
</bean>




<bean id="viewResolver"
    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>

下面是我的 web-applicationContext.xml

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


<bean class="com.mkyong.common.controller.WelcomeController" />
<bean class="com.mkyong.common.controller.HelloGuestController" />

</beans>

谁能纠正我哪里出错了。

4

1 回答 1

2

Add the following:

<mvc:annotation-driven/>
<context:component-scan base-package=""/>//the package where your controller are located

This will let you just use the controller annotation to locate you controllers.

于 2013-11-13T04:52:43.730 回答