Announcement Announcement Module
Collapse
No announcement yet.
JSF/Spring integration - managed-property problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSF/Spring integration - managed-property problem

    I am using JSF 1.1_01 (MyFaces 1.1), Spring 1.2, Ajax4Jsf.
    The JSF application has h:selectOneMenu .

    On change event of h:selectOneMenu sets "selectedValue" into backing bean as shown below:

    ------------------
    page.jsp
    -------------------

    <h:selectOneMenu value="#{test.selectedDevice}" >

    <f:selectItem itemValue="0" itemLabel="--New--"/>
    <f:selectItem itemValue="1" itemLabel="WorkStation"/>
    <f:selectItem itemValue="2" itemLabel="Router"/>
    <f:selectItem itemValue="3" itemLabel="Switch"/>
    <ajax:support action="#{test.loadDevice}" event="onchange" reRender="t2,t3,t4,t5"/>
    </h:selectOneMenu>

    -----------------------
    TestBean.java (Backing Bean)
    -----------------------

    public String getSelectedDevice() {
    logger.info(" *** In getSelectedDevice *** ");
    if (selectedDevice == null) {

    selectedDevice = "0"; // This will be the default selected item.
    }
    return selectedDevice;
    }


    public void setSelectedDevice(String selectedDevice) {
    logger.info(" *** In setSelectedDevice *** ");
    this.selectedDevice = selectedDevice;
    }

    Here are the configuration file snippets for integrating JSF Spring
    -------------
    web.xml
    -------------
    <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletCont extListener</listener-class>
    </listener>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    ---------------------------
    faces-config.xml
    ---------------------------
    <application>
    <variable-resolver>org.springframework.web.jsf.DelegatingVar iableResolver</variable-resolver>
    </application>

    <managed-bean>
    <managed-bean-name>test</managed-bean-name>
    <managed-bean-class>test.TestBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
    <property-name>deviceManager</property-name>
    <property-class> test.DeviceTypeManager </property-class>
    <value>#{deviceManager}</value>
    </managed-property>
    </managed-bean>

    The above code results in the following error

    javax.faces.FacesException: Cannot get value for expression '#{test.selectedDevice}'

    The error occurs only if i include <managed-property> inside the <managed-bean> in faces-config.xml.
    The moment i remove <managed-property> from face-config.xml the error disappears & page gets rendered properly.

    The purpose in having <managed-property> inside managed-bean is to call Spring's Manager class (i.e. deviceManager) from JSF application

    Any pointers/suggestions in resolving the error will be highly appreciated

    Regards
    Bansi

  • #2
    First this is used to inject your Managed bean with Spring configured beans. Your TestBean does not have a property of deviceManager. Need to have your TestBean have getters/setters for property DeviceManager.

    From http://static.springframework.org/sp...riableresolver
    The DelegatingVariableResolver will first delegate value lookups to the default resolver of the underlying JSF implementation, and then to Spring's 'business context' WebApplicationContext. This allows one to easily inject dependencies into one's JSF-managed beans.
    Second I don't believe you need the property-class node. The property-name is used to lookup a bean by its id.

    Code:
    <managed-property> 
       <property-name>deviceManager</property-name> 
       <value>#{deviceManager}</value> 
    </managed-property>
    The property-name is used to lookup a bean by its id. Spring config....

    Code:
    <bean id="deviceManager" class="test.DeviceTypeManager"/>
    Last edited by cwash5; Jan 7th, 2007, 01:52 PM.

    Comment


    • #3
      Hi Cales
      Thanks for your response.
      The TestBean do have a property for DeviceManager along with setter/getter methods as shown below. Sorry for not including in earlier posting

      --------------
      TestBean.java (Backing Bean)
      ---------------
      private DeviceTypeManager deviceManager;
      public DeviceTypeManager getDeviceManager() {
      return deviceManager;
      }


      public void setDeviceManager(DeviceTypeManager deviceManager) {
      this.deviceManager = deviceManager;
      }


      You are right i dont need to include <property-class> node. But sometimes it does help especially when things doesnt work i get little paranoid.

      I have successfully developed a CRUD prototype integrating JSF-Spring-Hibernate . But now things doesnt work me the moment i introduce <h:selectOneMenu> in JSF form. Here are the two scenarios

      Scenario 1 : without <managed-property> the code works fine

      Scenario 2 : with <managed-property> the code results in following error

      javax.faces.FacesException: Cannot get value for expression '#{test.selectedDevice}'


      Scenario 1 has only JSF whereas Scenario 2 has JSF-Spring integration

      The Scenario 1 works absolutely fine as the expression '#{test.selectedDevice}' gets its value from setter/getter method in the backing bean(TestBean.java) . This is expected behaviour & wondering why it doesn't work similarly in Scenario 2 instead complains
      Cannot get value for expression '#{test.selectedDevice}'



      I am willing to upload the war file. Any pointers/suggestions in resolving the error will be highly appreciated

      Regards
      Bansi

      Comment


      • #4
        In Scenario 1 how are you getting the deviceManager? I'm speculating that there is problem injecting in the deviceManager and therefore no testBean is available as a managedBean. Seems if this was the case there would be stacktrace or at least a warning in the log. Have you tried retrieving the bean directly from Spring using FacesContextUtils just to see if you have everything setup correctly?

        So..Remove your managed property and in your test bean do the following.

        Code:
        public DeviceManagerType getDeviceManager() {
          ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
          DeviceManagerType deviceManager = (DeviceManagerType) ctx.getBean("deviceManager");
          return deviceManager;
        }

        Comment


        • #5
          Thanks Caleb.
          Scenario 1 is pure JSF application that means i have not added deviceManager and <managed-property> in faces-config.xml.
          It has Only TestBean declared as <managed-bean> in faces-config.xml as shown below.

          <managed-bean>
          <managed-bean-name>test</managed-bean-name>
          <managed-bean-class>test.TestBean</managed-bean-class>
          <managed-bean-scope>request</managed-bean-scope>

          </managed-bean

          In Scenario 1 the expression "#{test.selectedDevice}" gets its value from the backing bean setter/getter methods . But in Scenario 2 it results in error
          Cannot get value for expression '#{test.selectedDevice}'

          The only additions to Scenario 2 are
          1) <managed-property> in faces-config.xml and
          2) defining "deviceManager" string along with setter/getter methods in backing bean TestBean.java

          Addition Code in Scenario 2:

          ------------------
          faces-config.xml
          ------------------

          <managed-property>
          <property-name>deviceManager</property-name>
          <property-class> test.DeviceTypeManager </property-class>
          <value>#{deviceManager}</value>
          </managed-property>



          -----------------
          TestBean.java
          ------------------

          private DeviceTypeManager deviceManager;
          public DeviceTypeManager getDeviceManager() {
          return deviceManager;
          }


          public void setDeviceManager(DeviceTypeManager deviceManager) {
          this.deviceManager = deviceManager;
          }

          Comment


          • #6
            I guess what I was trying to say was in Scenario2 try this...

            ------------------
            faces-config.xml
            ------------------
            Remove this.....
            <managed-property>
            <property-name>deviceManager</property-name>
            <property-class> test.DeviceTypeManager </property-class>
            <value>#{deviceManager}</value>
            </managed-property>



            -----------------
            TestBean.java
            ------------------

            Remove...
            private DeviceTypeManager deviceManager;
            Change....
            public DeviceTypeManager getDeviceManager() {
            ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesCo ntext.getCurrentInstance());
            DeviceManagerType deviceManager = (DeviceManagerType) ctx.getBean("deviceManager");
            return deviceManager;
            }

            Remove...
            public void setDeviceManager(DeviceTypeManager deviceManager) {
            this.deviceManager = deviceManager;
            }

            I wanted to see if replacing dependency injection with dependecy lookup changed the behavior. Also this will verify that the deviceManager bean is being created property and can be looked up from the WebApplicationContext. Of course to test this you need to call a property that needs the deviceManager.

            Comment


            • #7
              Hi Caleb
              Thanks for your thoughts. It just worked . The trick was i missed Spring Context loader listener in web.xml

              <listener>
              <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
              </listener>

              Thanks for all your help

              Regards
              Bansi

              Comment


              • #8
                To Bansi

                Hi there,
                Actually I m also doing the same work like trying to integrate spring into existing jsf application. well can you send the zip file of your simple example on my email [email protected] It would be really helpful to me because I m getting some weird errors and need some simple working application to understand how this integration works.
                I think your example would be good enough
                Regards,
                Sam

                Comment


                • #9
                  To Bansi

                  Hi Bansi,
                  Actually I m also doing the same work like trying to integrate spring into existing jsf application. well can you send the zip file of your simple example on my email [email protected] It would be really helpful to me because I m getting some weird errors and need some simple working application to understand how this integration works.
                  I think your example would be good enough
                  Regards,
                  Sam

                  Comment


                  • #10
                    Integrating Spring Web Flow with JSF

                    Comment


                    • #11
                      Code:
                      <managed-bean>
                      <managed-bean-name>test</managed-bean-name>
                      <managed-bean-class>test.TestBean</managed-bean-class>
                      <managed-bean-scope>request</managed-bean-scope>
                      
                      </managed-bean 
                      
                      In Scenario 1 the expression "#{test.selectedDevice}" gets its value from the backing bean setter/getter methods . But in Scenario 2 it results in error
                      Cannot get value for expression '#{test.selectedDevice}'
                      
                      The only additions to Scenario 2 are 
                      1) <managed-property> in faces-config.xml and
                      2) defining "deviceManager" string along with setter/getter methods in backing bean TestBean.java
                      
                      Addition Code in Scenario 2:
                      
                      ------------------
                      faces-config.xml
                      ------------------
                      
                      <managed-property>
                      <property-name>deviceManager</property-name>
                      <property-class> test.DeviceTypeManager </property-class>
                      <value>#{deviceManager}</value>
                      </managed-property> 
                      
                      
                      
                      -----------------
                      TestBean.java
                      ------------------
                      
                      private DeviceTypeManager deviceManager;
                      public DeviceTypeManager getDeviceManager() {
                      return deviceManager;
                      }
                      
                      
                      public void setDeviceManager(DeviceTypeManager deviceManager) {
                      this.deviceManager = deviceManager;
                      }


                      Hmm... It seems you are not passing "event" as a parameter to the method from which you are trying to retrive the data (say devices).
                      Try to pass the event object a parameter to the method "selectedDevice"

                      Might be this help.

                      Comment

                      Working...
                      X