This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSF and the fly likes Integration problems using h:selectOneMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Integration problems using h:selectOneMenu" Watch "Integration problems using h:selectOneMenu" New topic
Author

Integration problems using h:selectOneMenu

Bansilal Haudakari
Greenhorn

Joined: Jun 28, 2004
Posts: 15
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" in 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 files for integrating JSF Spring
-------------
web.xml
-------------
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</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.DelegatingVariableResolver</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 using <managed-property> is to integrate JSF with Spring i.e. calling deviceManager

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

Regards
Bansi
Bansilal Haudakari
Greenhorn

Joined: Jun 28, 2004
Posts: 15
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;
}


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
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hello,
I am using the bean name to get the value in h:selectOneMenu.
Here is my code, which is working fine.....
In JSf page,

<h:selectOneMenu id="usage" value="#{registrationBean.usage}" styleClass="labelTextBlack"
<f:selectItems value="#{registrationBean.usageList}"/>
<a4j:support event="onchange" action="#{registrationBean.changeUsage}" reRender="plateType,plateClass,expiration,issueYear1"></a4j:support>
</h:selectOneMenu>

The Backing Bean has....

public class RegistrationBean extends BaseManagedBean implements Serializable {
private RegistrationService registrationService;

private ActivityInfoBean activityInfoBean;

public RegistrationService getRegistrationService() {
return registrationService;
}

public void setRegistrationService(RegistrationService registrationService) {
this.registrationService = registrationService;
}

public ActivityInfoBean getActivityInfoBean() {
return activityInfoBean;
}

public void setActivityInfoBean(ActivityInfoBean activityInfoBean) {
this.activityInfoBean = activityInfoBean;
}

public String getUsage() {
return usage;
}

public void setUsage(String usage) {
this.usage = usage;
}

}

In faces-config....

<managed-bean>
<managed-bean-name>registrationBean</managed-bean-name>
<managed-bean-class>us.tn.state.trust.presentation.beans.RegistrationBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>activityInfoBean</property-name>
<value>#{activityInfoBean}</value>
</managed-property>
<managed-property>
<property-name>registrationService</property-name>
<value>#{registrationService}</value>
</managed-property>
</managed-bean>


cheers
Rajani
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
sorry, I forgot to include the web.xml and applicationContext.xml contents...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>

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

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

</web-app>

and you have to include the spring bean in applicationContext.xml as...

<?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="registrationService" class="us.tn.state.trust.service.RegistrationServiceImpl">
</bean>

</beans>

Cheers,
Rajani
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
In web.xml, you have to define the listener so that Spring initializes itself on application startup:


and


for defining the faces-config.xml

Cheers,
Rajani
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integration problems using h:selectOneMenu