<h:form>
<h:selectManyMenu id="carsList"
value="#{bean.carList}"
style="width:400px; height:100px" label="List">
<f:selectItems value="#{bean.cars}" var="i"
itemLabel="#{i.code}" itemValue="#{i.Name}" />
<f:selectItem itemLabel="Other" itemValue="other"/>
<f:ajax event="change" execute="@this" render="othermodel"/>
</h:selectManyMenu>
<br></br>
<h:panelGroup id="othermodel">
<h:outputText value="Others:" style="margin-top:10px;color:red;font-weight:bold;"
rendered="#{bean.carList.contains('other')}" />
<h:inputText size="50" id="otherinput" required="true"
rendered="#{bean.carList.contains('other')}"/>
<h:message for="otherinput"></h:message>
</h:panelGroup>
<h:commandButton value="Next" action="#{bean.submitForm}"/>
</h:form>
My bean is requestScoped
and when my carList
has a value other
i am able to show the panelGrid
but when user don't enter any value in the input field rendered using AJAX
, even i specificed required=true
but it's not getting validated. And value of the input text box is null in the backend code.
How can I do the validation for the input field rendered using AJAX
and why my value is coming as null? I am using JSF 2.0
The rendered
attribute is re-evaluated during the request of the form submit. Your bean is request scoped and thus recreated on every single request (also ajax requests) with all properties set to default. So the carList
property will also be emptied and the rendered
attribute would evalute false
before the update model values phase has filled the carList
. So, for JSF, the <h:inputText>
is never rendered and thus it won't be processed.
Put the bean in the view scope instead. It'll live as long as you interact with the same view by (ajax) postbacks. It's the right scope for dynamic ajax based forms with a lot of conditional rendering.
I tried this with @ViewScope
. When I made my Spring managed service
as transient I was getting null
reference after deserialization. So, i tried the below method to get the Spring Service
.
@ManagedBean(name="bean")
@ViewScoped
public class Bean implements Serializable {
@ManagedProperty(value="#{appService}")
private transient AppService appService;
// Getting AppService Object which is singleton in the application during deserialization
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
FacesContext context = FacesContext.getCurrentInstance();
appService = (AppService)context.getApplication()
.evaluateExpressionGet(context, "#{appService}", AppService.class);
stream.close(); // not sure i have to do this
}
}
This is working for me without any issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.