Results 1 to 9 of 9

Thread: BeanNotOfRequiredTypeException when chaining views

  1. #1
    Join Date
    Aug 2004
    Location
    Bridgewater, NJ
    Posts
    10

    Default BeanNotOfRequiredTypeException when chaining views

    I was able to recreate this exception using the webapp-minimal in the included Spring samples directory.

    Here's what I did:

    1) Chained the following ViewResolvers:
    InternalResourceViewResolver
    XmlViewResolver

    2) Created a bean that extended from AbstractXsltView so I can use XSLT.

    3) Created a bean that extended from SimpleFormController so I can have a JSP form.


    Here's the results:

    1) The included test.jsp displays fine (this extends from Controller).

    2) The XSLT view displays fine.

    3) I get BeanNotOfRequiredTypeException for the JSP form (this extends from SimpleFormController). When I went to debug, the loadView() in XMLViewResolver is able to "see" this bean and thus attempts to display it. I expected it to return null so the InternalResourceViewResolver can handle it.

    When I commented out the XmlViewResolver, the JSP form displays just fine!

    So, the chaining works for beans extending Controller but not SimpleController. Did I miss something?

    Any ideas??? :cry:

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    XmlViewResolver will also look in the applicationContext for beans of that type.

    Can you post your appContext, servlet-context and your view definitions please.

  3. #3
    Join Date
    Aug 2004
    Location
    Bridgewater, NJ
    Posts
    10

    Default

    May I ask: how did you know that the XmlViewResolver will look in the applicationContext? I didn't see this in any docs. If that's the case where can I define my JSP views so XmlViewResolver would not see it?

    I don't have appContext file. Here's the servlet-context file:
    NOTE: The "/transmitLead.htm" path is the one that throws the exception.


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="xsltViewResolver" class="org.springframework.web.servlet.view.XmlVie wResolver">
    <property name="order" value="1"/>
    <property name="location" value="/WEB-INF/views.xml"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="viewClass"><value>org.springframework.web.se rvlet.view.JstlView</value></property>
    <property name="prefix"><value>/WEB-INF/jsp/</value></property>
    <property name="suffix"><value>.jsp</value></property>
    </bean>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/test.htm">exampleController</prop>
    <prop key="/transmitLead.htm">transmitLeadForm</prop>
    <prop key="/receiptConfirmation.htm">receiptConfirmationContro ller</prop>
    </props>
    </property>
    </bean>

    <bean id="exampleController" class="example.ExampleController"/>

    <bean id="receiptConfirmationController" class="example.ReceiptConfirmationController"/>

    <bean id="transmitLeadForm" class="example.TransmitLeadForm">
    <property name="formView">
    <value>transmitLeadForm</value>
    </property>
    <property name="successView">
    <value>waitResponse</value>
    </property>
    </bean>

    </beans>

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Use the source Luke

    If you read the source for XmlViewResolver, it adds the applicationContext (I think) as the parent. Do not have it with me at the moment, but I remember looking because someone else asked a few days ago.

    Will find it tomorrow at work if you want me to.

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

  6. #6
    Join Date
    Aug 2004
    Location
    Bridgewater, NJ
    Posts
    10

    Default

    I beg to disagree. If you look at this line under initFactory():

    reader.loadBeanDefinitions(actualLocation);

    By me supplying the actualLocation via setLocation(), it will be loading the beans from that configuration file. Am I wrong?

  7. #7
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Maybe I mis-understand the behaviour of the bean factory which is very possible My understanding is that a beanFactory will look in it's current context *and it's parent context* for a bean.

    The code

    Code:
    // Create child ApplicationContext for views.
    		GenericWebApplicationContext factory = new GenericWebApplicationContext&#40;&#41;;
    		factory.setParent&#40;getApplicationContext&#40;&#41;&#41;;
    		factory.setServletContext&#40;getServletContext&#40;&#41;&#41;;
    constructs a factory which has visible of the applicationContext. Sure, the link
    Code:
    reader.loadBeanDefinitions&#40;actualLocation&#41;;
    specificies your actualLocation, but the reader is constructed with that parent factory.

    My interpretation is that "loadBeanDefinitions" will *add* the beans defined in the specified location, but you still have access to the parent context.

    Maybe one of the spring team could answer this?

  8. #8
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Just some further reading:

    In contrast to the methods in ListableBeanFactory, all of the methods in this interface will also check parent factories if this is a HierarchicalBeanFactory. If a bean is not found in this factory instance, the immediate parent is asked. Beans in this factory instance are supposed to override beans of the same name in any parent factory.
    from http://www.springframework.org/docs/...anFactory.html

    and indeed http://www.springframework.org/docs/...anFactory.html implements http://www.springframework.org/docs/...anFactory.html

    HTH.

  9. #9
    Join Date
    Aug 2004
    Location
    Bridgewater, NJ
    Posts
    10

    Default

    Thanks yatesco. I decided to just switch to ResourceBundleViewResolver so I can declare all my views there. After I did that everything worked.

Similar Threads

  1. Replies: 22
    Last Post: Feb 4th, 2010, 01:37 AM
  2. AbstractWizardController views
    By avp12 in forum Web
    Replies: 0
    Last Post: Mar 30th, 2005, 04:51 PM
  3. Multiple views and pages question
    By jwray in forum Swing
    Replies: 3
    Last Post: Feb 19th, 2005, 09:22 AM
  4. Newbie question on Pages and Views
    By ragnarwestad in forum Swing
    Replies: 8
    Last Post: Dec 13th, 2004, 10:47 PM
  5. How to map views
    By pak in forum Web
    Replies: 4
    Last Post: Sep 8th, 2004, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •