2012-09-20 32 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

相关问题