Not sure if anyone's answered this yet, but what scope is your backing bean?
When the value-change event calls hostSelection, you are setting
this.hostId on your backing bean, which is then used by getCpuIds to
build the list of options for the other select. If your bean is
request-scoped, when you submit the form, this.hostId will be null
because the form submit is a different HTTP request, and nothing sets it
again. Likewise, the validation rule for <h:select*> runs against the
options that are valid in the scope of the current request, not the
options that were in the form when it was rendered; in request-scope,
that list is also re-generated, and will not contain the valid values if
hostId is null. Hence the error you're seeing.
And even if hostId was set from the #{bean.user} binding expression, it
might not be enough because the validation phase comes before the apply
request values/value-binding phase.
So your two possible workarounds are to make the bean session-scoped, or
to ensure that something pushes the value back on a full form-submit.
something like <t:saveState value="#{bean.hostId}"/> would work because
saveState applies its state earlier in the JSF lifecycle before validations.
Hope this helps!
-- Bill
Hi,
i am using two selectonemenus in my application
one selectone is for host (system)
second selectone is for cpu (cpus of the system)
if i select first host (any hostname)the value change listeners fires and
it gives
the corresponding cpus of the host from the database and sets to second
selectonemenu .
but when i submitted to a next form
i am getting a error
Value is not a valid option
This is jsp code.
<h:outputText value="SelectHOST "/></th>
<h:panelGroup>
<h:selectOneMenu id="hostids" value="#{HRselforeachcpuanduser.users}"
valueChangeListener="#{HRselforeachcpuanduser.hostSelection}"
onchange="submit();"immediate="true" required="true">
<f:selectItem itemValue="" itemLabel=""/>
<f:selectItems value="#{HRselforeachcpuanduser.hosts}"/>
</h:selectOneMenu>
<h:message for="hostids" style="color:red"/>
</h:panelGroup> <br>
<h:outputText value="SelectCPU" />
<h:panelGroup>
<h:selectOneMenu id="cpuids" value="#{HRselforeachcpuanduser.cpuid}" >
<f:selectItems value="#{HRselforeachcpuanduser.cpuids}"/>
</h:selectOneMenu>
<h:message for="cpuids" style="color:red"/>
</h:panelGroup>
Corresponding java code
// this is for value change listener
public void hostSelection(ValueChangeEvent e) {
FacesContext context = FacesContext.getCurrentInstance();
hostid=(String) e.getNewValue();
getCpuids();
context.renderResponse();
}
//this is for host select
public Map getHosts(){
try{
openConnection();
selhostnames=new HashMap();
String str="select distinct hostid from system_cpu";
st=con.createStatement();
rs=st.executeQuery(str);
while(rs.next()){
hostname=rs.getString("hostid");
System.out.println(hostname);
selhostnames.put(hostname,hostname);
}
return selhostnames;
}
public String getHost(){
return hostselected;
}
public void setHost(String hostselected){
this.hostselected = hostselected;
}
//this is for cpu selection
public Map getCpuids(){
try{
if(hostid==null){
System.out.println("if it is null"); // imp note
return selcpunames;
}
openConnection();
String str="select distinct cpuid from system_cpu where hostid=?";
System.out.println("setting cpuids from host");
ps=con.prepareStatement(str);
ps.setString(1,hostid);
ps.execute();
rs=ps.getResultSet();
while(rs.next()){
cpuname=rs.getString("cpuid");
System.out.println(cpuname);
selcpunames.put(cpuname,cpuname);
}
return selcpunames;
}
public void setCpuid(String cpuid){
this.cpuid=cpuid;
}
public String getCpuid(){
return cpuid;
}
--
Bill Schneider
Chief Architect
Vecna Technologies
5004 Lehigh Rd.
College Park, MD 20740
[EMAIL PROTECTED]
t: 240-737-1640
f: 301-699-3180