aspose file tools*
The moose likes JSF and the fly likes Issue with jsf value change listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Issue with jsf value change listener" Watch "Issue with jsf value change listener" New topic
Author

Issue with jsf value change listener

sathez kumar
Greenhorn

Joined: Jul 18, 2012
Posts: 2
I am using jsf2.0 with primefaces. In my application there is one registration form ,in that i have city and state field.. when i am select the state it automatically retrieve the corresponding city on the city field, after selecting the city i am always getting the error message like 'City: Validation Error: Value is not valid';


here is my xhtml code..

<hutputText value="State*"></hutputText>

<p:selectOneMenu id="currentState"
value="#{volunteerBean.currentState}" required="true"
immediate="true" requiredMessage="Select your state" valueChangeListener="#{functions.valuechagemethod}"
label="State" style="width:196px">
<f:ajax execute="@this" render="currentCity"></f:ajax>
<f:selectItem itemLabel="select" itemValue=""></f:selectItem>
<f:selectItems value="#{volunteerBean.stateList}" var="item"
itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
<p:ajax event="change" update="msg16" />
</p:selectOneMenu>
<h:message for="currentState" id="msg16" styleClass="alert" />
<p:tooltip for="currentState" value="Select Your State" style="color:blue;font-size:80%;" showEffect="fade" hideEffect="fade"/>

<hutputText value="City*"></hutputText>
<p:selectOneMenu id="currentCity"
value="#{volunteerBean.currentCity}" required="true"
immediate="true" requiredMessage="Select your city"
label="Country" style="width:196px">
<f:selectItem itemLabel="Select" />
<f:selectItems value="#{functions.retList}" var="item"
itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
<p:ajax event="change" update="msg15" />
</p:selectOneMenu>
<h:message for="currentCity" id="msg15" styleClass="alert" />
<p:tooltip for="currentCity" value="Select Your City" style="color:blue;font-size:80%;" showEffect="fade" hideEffect="fade"/>




here is my bean method

public void valuechagemethod(ValueChangeEvent e)
{
String sval=e.getNewValue().toString();
Session session = null;
SessionFactory sessionFactory = HibernateUtil.getInstance();
session = sessionFactory.getCurrentSession();
Transaction tr= session.beginTransaction();
long sid=(Long) session.createQuery("select state_id from
StateBean where state_name='"+sval+"'").uniqueResult();
retList = session.createQuery("select city_name from CityPojo
where state_id='"+sid+"'").list();
setRetList(retList);
tr.commit();
}

public List<VolunteerBean> getRetList() {
return retList;
}

public void setRetList(List<VolunteerBean> retList) {
this.retList = retList;
}
public List<VolunteerBean> retList;






[Thumbnail for u8Vr0.png]

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Welcome to the JavaRanch, Sathez! You can make your code and XML more readable using this advice: http://www.coderanch.com/how-to/java/UseCodeTags

Selection controls will throw a validation error when the list of valid values (as defined by its selectItems) doesn't contain the selected value. This can happen, for example, when the backing bean was defined with Request scope (which is almost entirely useless in JSF).

You coded "immediate=true" on your control, however, and that can be a problem, since "immediate" doesn't mean "right now", it means "submit the form, but not the data values". Which means that your updated control value won't be sent to the server.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
Consider Paul's rocket mass heater.
 
subject: Issue with jsf value change listener