C: choose not work in JSF

I have three values, and I want one component to be displayed in the case of the first two values, and another component for the third value

Below, I have my page:

page.xhtml

<ui:repeat value=#{bean.value} var="data"> <c:choose> <c:when test="#{data.thirdValue == 'content'}"> <h:outputText value="Wrong value"/> </c:when> <c:otherwise> Correct! </c:otherwise> </c:choose> </ui:repeat> 

This is how my page is defined. I also added the following namespace: xmlns:c="http://java.sun.com/jsp/jstl/core .

I tested whether the value specified inside the test for <c:when> returns "true" or "false" and it does.

My problem is that <c:when> never evaluated. The value <c:otherwise> always displayed. Am I missing something? Is this because conditional rendering is inside <ui:repeat> , so when is it not being evaluated?

Any help would be greatly appreciated. thanks in advance

+6
source share
2 answers

You need to remove c: choose, because, as @Jens says in the comments, they are processed in different phases. You can use JSTL in tandem with JSF, but you must respect the order in which they are resolved. read the delicate answers of BalusC, it sways.

As for your needs, you can use the attribute to conditionally output your text:

 <ui:repeat value="#{bean.value}" var="data"> <h:outputText rendered="#{data.thirdValue == 'content'}" value="Wrong value"/> <h:outputText rendered="#{data.thirdValue != 'content'}" value="Correct!"/> </ui:repeat> 
+9
source

To add a Mindwin answer, you need to understand that <c:choose> is a tag handler when <ui:repeat> is a user interface component. The first is evaluated when building the component tree, and the second is when viewing the view, that is, at a later time. In this light, the dependence on var on <ui:repeat> is what is wrong with your code, because it was not evaluated when you entered the game <c:choose> .

Two things to remember here.

Use the iterative tag handler, <c:forEach> , with your content:

 <c:forEach items=#{bean.values} var="data"> <c:choose> <c:when test="#{data.thirdValue == 'content'}"> <h:outputText value="Wrong value"/> </c:when> <c:otherwise> Correct! </c:otherwise> </c:choose> </c:forEach> 

With this approach, both tags are launched simultaneously (the view is built), so conflicts will not occur. Just remember that if your bean is viewable, it will be recreated for every request.

Use the user interface component with the rendered attribute:

 <ui:repeat value=#{bean.values} var="data"> <h:outputText rendered="#{data.thirdValue == 'content'}" value="Wrong value"/> <ui:fragment rendered="#{data.thirdValue != 'content'}"> <h1>Correct</h1> </ui:fragment> </ui:repeat> 

In this case, everything starts simultaneously (the view is visualized). Note that you can display a whole bunch of HTML, such as <ui:fragment> , as well as some specific JSF tags, such as <h:outputText> , using the rendered attribute.


Ultimately, continue the classic post: JSTL in JSF2 Facelets ... makes sense? to get a complete understanding of the relationship between tag handlers and user interface components.

+9
source

Source: https://habr.com/ru/post/945915/


All Articles