2012-01-04 47 views
2

我有一个现有的Spring应用程序,它执行一些服务器端处理。我正在尝试为这个特定的应用程序创建一个webapp,并选择SpringMVC来为我的目的服务。将* -servlet.xml与applicationContext.xml混合使用

我创建了一个显示控制器如下:

@Controller 
@RequestMapping("/items") 
public class ItemDisplayController { 
    private static final Logger LOGGER = Logger.getLogger(ItemDisplayController.class); 
    private static final String ITEMS_REDIRECT = "redirect:/item/items"; 

    @Autowired 
    private ItemDisplay itemDisplay; 

    @RequestMapping 
    public String listItems(ModelMap model) { 
     if (LOGGER.isInfoEnabled()) { 
      LOGGER.info("List all items"); 
     } 

     List<ItemDetail> itemDetails = itemDisplay.getAllItems(); 
     model.addAttribute("itemDetails",itemDetails); 
     return "items"; 
    } 
} 

我已经有以下定义一个ApplicationContext文件:

<?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:mvc="http://www.springframework.org/schema/mvc" 
     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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="locations"> 
      <array> 
       <value>classpath:item.properties</value> 
       <value>file:#{systemEnvironment['ITEM_HOME']}/item.properties</value> 
       <value>file:#{systemProperties['ITEM_HOME']}/item.properties</value> 
      </array> 
     </property> 
     <property name="ignoreResourceNotFound" value="true"/> 
    </bean> 


    <bean id="itemDisplay" class="com.acme.itemDisplayImpl"> 
      <property name="itemDisplayDAO" ref="jdbcItemDisplayDAO"/> 
     </bean> 

    <bean id="jdbcItemDisplayDAO" class="com.acme.database.dao.JdbcItemDisplayDAO"> 
     <property name="dataSource" ref = "dataSource"/> 
    </bean> 


    <bean id="realDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="#{props['itemds.jdbc.driver']}"/> 
     <property name="url"><value><![CDATA[#{props['itemds.jdbc.url']}]]></value></property> 
     <property name="username" value="#{props['itemds.username']}"/> 
     <property name="password" value="#{props['itemds.password']}"/> 
     <property name="testOnBorrow" value="#{props['itemds.test.on.borrow']}"/> 
     <property name="testWhileIdle" value="#{props['itemds.test.while.idle']}"/> 
     <property name="poolPreparedStatements" value="#{props['itemds.pool.prepared.statements']}"/> 
     <property name="validationQuery" value="#{props['itemds.validation.query']}"/> 
     <property name="validationQueryTimeout" value="#{props['itemds.validation.query.timeout']}"/> 
     <property name="timeBetweenEvictionRunsMillis" value="#{props['itemds.time.between.eviction.runs.millis']}"/> 
     <property name="maxActive" value="#{props['itemds.max.active']}"/> 
     <property name="maxIdle" value="#{props['itemds.max.idle']}"/> 
     <property name="initialSize" value="#{props['itemds.initial.size']}"/> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> 
     <property name="targetDataSource" ref="realDataSource"/> 
    </bean> 

</beans> 

在我* 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" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     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/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <mvc:annotation-driven/> 
    <context:annotation-config/> 
    <context:component-scan base-package="com.acme.item"/> 

    <bean id="primaryViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
     <property name="contentType" value="text/html; charset=UTF-8"/> 
     <property name="order" value="1"/> 
    </bean> 
</beans> 

而在web.xml中,我定义了上下文参数:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     version="2.5" 
     xmlns="http://java.sun.com/xml/ns/javaee"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 

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

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

    <servlet-mapping> 
     <servlet-name>items</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

当我运行我的应用程序时,itemDisplay没有连线并且在调试器上显示为空值。

有人可以指出我可能在这里做错了吗?在理想的世界中,我会假设在控制器中使用@Autowired注释itemDisplay将有助于解析接口的实现。

+0

* servlet.xml的全名是什么? – soulcheck 2012-01-04 15:33:21

+0

日志中有趣的事情? – Bozho 2012-01-04 15:34:00

+0

@soulcheck - 它是item-servlet.xml – 2012-01-04 15:51:17

回答

1

我能想到的只有一点,ContextLoaderListener的不抱怨,如果没有找到applicationContext.xml中,让我们试试这个添加的classpath *:applicationContext.xml的也确保applicationContext.xml的运行时类路径服务器

这是一篇很好的文章,提供了一个很好的洞察弹簧classpath资源。

+0

您提供的链接已损坏。 – 2012-01-08 00:36:16

+0

对不起,修正了它。 – 2012-01-08 01:50:59

1

您的文件在快速浏览时看起来很好。只要在上下文中只有一个具有ItemDisplay类型的bean,它应该按类型自动装配,否则应该在日志文件中出现错误。

我注意到的一件小事,可能与您的特定问题无关,是您将'items'控制器映射到web.xml中的所有文件。通常你会希望它映射到特定类型的文件只有 - 说的* .htm,这样就不会被调用的资源,如图片等

1

不知道这是你的applicationContext文件错字:

<bean id="itemDisplay" class="com.acme.itemDisplayImpl"/>

不应该是com.acme.item.DisplayImpl?类。如果类的名称确实是itemDisplayImpl,那么您需要更改*servlet.xml中的组件扫描元素以包含正确的包。