0

Trying to display the pages dynamically based on the dropdown list value.All the associated components are rendering properly.When I bean is in view scope the validations are not triggering whereas the samething is working fine with session scope.Could anyone please help me to resolve the issue?

Here follows my code of Main.xhtml This page contains the dropdownlist.Based on the dropdown value dynamically including the pages.

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="#{templates.standard}">
<ui:define name="contentArea">
<c:choose>
<c:when test="#{testBean.value == '1'}">
<h:panelGroup>
<ui:include src="Page1.xhtml" />
</h:panelGroup>
</c:when>
<c:when test="#{testBean.value == '2'}">
<h:panelGroup>
<ui:include src="Page2.xhtml" />
</h:panelGroup>
</c:when>
</c:choose>
</ui:define>
</ui:composition>
</html>

The below Page1.xhtml will be included dynamically in Main.xhtml 

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:panelGroup>
<h:inputText value="#{testBean1.date}"
id="date" 
required="true" requiredMessage="Enter Valid date">
<f:validator validatorId="test.TestDateValidator" />        
</h:inputText>  
</h:panelGroup>
<h:panelGroup>
<h:message for="date"/>
</h:panelGroup>
Sathya Elangovan
  • 163
  • 3
  • 14

1 Answers1

4

View scoped beans are stored in the JSF view. The JSF view is only available when it has been built. Taghandlers like JSTL <c:xxx> run during view build time. They thus run before the JSF view is available. So, when you bind a property of a view scoped bean to a JSTL tag attribute, then it would not refer the view scoped bean instance in the JSF view, but instead refer a freshly created one, with all properties set to default.

So, basically, you end up with two different instances of a view scoped bean on a per-request basis. One which is used during restoring the view (the one which is freshly recreated on every form submit) and another one which is used during processing the form submit (the one which was actually stored in the view scope).

This chicken-egg issue is already reported as JSF issue 1492 and fixed for the upcoming JSF 2.2.

Until then, your best bet is to create a separate request scoped bean and let the include condition depend on a request parameter which is injected by @ManagedProperty, or to turn off partial state saving (which may however have memory/performance implications). Note that the <ui:include> also runs during view build time, so wrapping it in a JSF component with the rendered attribute won't help anything as it is evaluated during view render time.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Just updating from BalusC answer to http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense "Do not bind attributes of view scoped beans to JSTL tag attributes when using Mojarra version older than 2.1.18", see also comment at bottom of https://java.net/jira/browse/JAVASERVERFACES-1492 "balusc 13/Apr/13 .. I can't reproduce this problem on 2.1 anymore since 2.1.18. Since this version, taghandlers started to work fine together with view scoped beans. I'm not sure which 2.1.18 issue exactly fixed it for 2.1 as well (note that this ticket was targeted at 2.2)." – Webel IT Australia - upvoter May 23 '13 at 09:33