错误表明springmvc-servlet.xml是错误的但是在检查时看起来是对的吗?

时间:2009-10-31 04:10:55

标签: spring configuration spring-mvc

我正在学习Spring并构建一个示例应用程序。

我收到错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:Bean名称'/list_cars.html'已在此文件中使用 违规资源:ServletContext资源[/WB-INF/springmvc-servlet.xml]

我之前有一个项目的类似错误,该项目有一个springmvc-servlet.xml文件,此错误为真。但是当我用下面的文件替换它(并删除了以前的项目并重新启动Tomcat)时,我继续得到错误,任何帮助将不胜感激。

<?xml version="1.0" encoding="UTF-8"?>

 <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">

 <!-- beans -->
<bean id="carManager" class="springmvc.service.CarManager">
   <property name="carList">
     <list>
        <ref bean="car1"/>
        <ref bean="car2"/>
    </list>
    </property>
</bean>    

<bean id="brandManager" class="springmvc.service.BrandManager">
  <property name="brandList">
     <list>
        <ref bean="brand1"/>
        <ref bean="brand2"/>
    </list>
  </property>
</bean>

<bean id="brand1" class="springmvc.model.Brand">
    <property name="id" value="1"/>
    <property name="name" value="Mercedes"/>
    <property name="country" value="Germany"/>
</bean>

<bean id="brand2" class="springmvc.model.Brand">
    <property name="id" value="2"/>
    <property name="name" value="Peugeot"/>
    <property name="country" value="France"/>
</bean>

<bean id="car1" class="springmvc.model.Car">
    <property name="id" value="1"/>
    <property name="brand" ref="brand1"/>
    <property name="model" value="SL 500"/>
    <property name="price" value="40000"/>
</bean>

<bean id="car2" class="springmvc.model.Car">
    <property name="id" value="2"/>
    <property name="brand" ref="brand2"/>
    <property name="model" value="607"/>
    <property name="price" value="35000"/>
</bean>

 <!-- urls -->
<bean name="/hello_world.html" class="springmvc.web.HelloWorldController"/>

<bean name="/list_cars.html" class="springmvc.web.CarListController">
    <property name="carManager" ref="carManager"/>
</bean>

<bean name="/new_car.html" class="springmvc.web.CarNewController">
    <property name="commandClass" value="springmvc.model.Car"/>
    <property name="formView" value="carNew"/>
    <property name="successView" value="list_cars.html"/>
    <property name="validator">
        <bean class="springmvc.validator.CarValidator"/>
    </property>
    <property name="carManager" ref="carManager"/>
    <property name="brandManager" ref="brandManager"/>
</bean>


 <!-- misc -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

 </beans>

1 个答案:

答案 0 :(得分:1)

为了使事情更具可读性,在定义bean时使用正确的bean名称,并将url名称放在urlMapping属性中,如下所示,以便为控制器创建url映射。

<bean name="carListController" class="springmvc.web.CarListController">
        <property name="carManager" ref="carManager"/>
</bean>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
            <props>
        <prop key="/list_cars.html">carListController</prop>

            </props>
        </property>
    </bean>