开发者

Error suggests springmvc-servlet.xml is wrong but on inspection it looks right?

I am learning Spring and building a sample app.

I am getting the error:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name '/list_cars.html' is already used in this file Offending resourc开发者_开发百科e: ServletContext resource [/WEB-INF/springmvc-servlet.xml]

I previously got a similar error for a project which had a springmvc-servlet.xml file for which this error was true.But when I replaced it with the file below (and deleted the previous project and restarted Tomcat) I continue to the get the error, any help would be much appreciated.

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


Just to make things more readable, use proper bean name when defining a bean and put url name in the urlMapping properties like below to make a url map to a controller.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜