2012-09-20 33 views
0

我正在學習如何使用@MVC,現在我對配置文件「...- servlet.xml」和「applicationContext.xml」感到困惑。以下可以工作,如果您能指出什麼是多餘或放錯位置,我將不勝感激。例如,我如何避免重複控制器包掃描?-servlet.xml和applicationContext.xml的正確配置聲明是什麼?

mrpomario-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     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:annotation-config/> 

    <context:component-scan base-package="mrpomario.springcore.mvc.controller"/> 

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

</beans> 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     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/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <context:annotation-config/> 

    <context:component-scan base-package="mrpomario.springcore.mvc.controller, mrpomario.springcore.mvc.dao, mrpomario.springcore.mvc.service"/> 

    <tx:annotation-driven/> 

    <jdbc:embedded-database id="dataSource" type="H2"> 
     <jdbc:script location="/WEB-INF/mvc-db.sql"/> 
    </jdbc:embedded-database> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="packagesToScan"> 
      <list> 
       <value>mrpomario/springcore/mvc/domain</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"/> 

</beans> 

的web.xml

<?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_2_5.xsd" 
     id="WebApp_ID" version="2.5"> 

    <display-name>MrPomario</display-name> 

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

    <servlet-mapping> 
     <servlet-name>mrpomario</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

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

</web-app> 

回答

0

理由: 春天-config.xml文件 - >看到豆 - > applicationContext.xml中但不反之亦然。

當我將控制器掃描移到spring-confing.xml中時,它仍然無法工作。是什麼阻止它發揮作用的是,我一直在applicationContext.xml如下:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"/> 

這些(在applicationContext.xml中),因爲他們是在春天-config.xml中定義不掃描我的控制器的@RequestMappings因此無法看到。

1

的applicationContext將不同的彈簧servlet之間如果它們存在(通常被共享只有一個)。

爲避免重複,只需從其中一個文件中刪除。

如果它導致問題,您的web-inf/web.xml可能無法正確設置。它需要引用正確的文件來配置上下文。

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

同意;我將它從applicationContext.xml中移除(僅在-servlet.xml中保留它)。儘管如此,通過這樣做,我的映射停止工作(找不到使用URI [feed/list]的HTTP請求的映射)。那麼,如何解決這個問題? – Pomario

+0

web-inf/web.xml沒有正確引用-servlet.xml? – NimChimpsky

+0

我相信web-inf/web.xml正確地引用了mrpomario-servlet.xml。我只是把它放在我的問題中。 – Pomario

相關問題