While-Loop in JSF - jsf-2

i want just a simple while-loop in JSF.
Something like :
<c:set var="i" value="0"/>
<c:while #{act.observation[i].value !=null}>
<td style="width: 12%;">#{act.observation[i]} /></td>
<c:set var="i" value="${i + 1}" />
Of course i know there is nothing like c:while and i have tried c:forEach and ui:repeat, but couldn't figure out how this could have worked, especially with xml paths (rims) that i am using.
Any ideas?

Try something like this:
<ui:repeat value="#{act.observation}" var="observ">
#{observ}<br/>
</ui:repeat>
Where observation is your list of items you want to print.

Related

Display JSF element only if specific h:message is being displayed

I have a h:form with several inputs and each of them got its own h:message and I'm looking for a way to show (using render or assigning some styleClass) some other element only when specific h:message is being shown (and hide when that h:message is not being displayed).
Here a code snippet
<li>
<h:panelGroup id="current_password_wrapper">
<p:password value="#{myBean.myCurrPass}" id="current_password" required="true"
styleClass="required_field" />
</h:panelGroup>
<h:message for="current_password"/>
</li>
<li>
<h:panelGroup id="new_password_wrapper">
<p:password value="#{myBean.myNewPass}" id="new_password" required="true"/>
</h:panelGroup>
<h:message for="new_password"/>
<h:commandLink value="my value"/>
</li>
I want to make the h:commandLink visible only when the <h:message for="new_password"/> is being displayed
So far I couldn't find anything...
If your environment supports EL 2.2, then you could check if FacesContext#getMessageList() isn't empty for the particular client ID.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not empty facesContext.getMessageList(new_password.clientId)}" />
If the message is being shown as result of a validation error, then you could also just check the UIInput#isValid() state of the component associated with the message.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not new_password.valid}" />
Note that manually adding a faces message to the context won't mark the input component invalid. Therefor either a true Validator should be used which throws a ValidatorException, or an explicit input.setValid(false) call has to be done programmatically.
I think with the answer to this question you requirement can be archived:
How to number JSF error messages and attach number to invalid component
I think you can do something like this:
<h:outputText value="output text" rendered="#{bean.messageIndexes['form:input1'] > 0}" />

<c:if><c:otherwise> does not work, both conditions evaluate like true

I am trying to toggle between two different command buttons depending on whether a list contains the given item ID:
<c:if test="#{roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
<p:commandButton ajax="true" id="commanddeactivate" update=":roomserviceForm:hoteltable,:roomserviceForm:msgs" actionListener="#{roomServiceManagerBean.deactivateServiceForHotel(hotel.hotelvillaId)}" icon="ui-icon-radio-off" type="submit" value="Remove Service">
<f:param name="roomserviceid" value="#{roomServiceManagerBean.roomServiceId}" />
</p:commandButton>
</c:if>
<c:otherwise>
<p:commandButton id="commandactivate" update=":roomserviceForm:hoteltable,:roomserviceForm:msgs" actionListener="#{roomServiceManagerBean.activateServiceForHotel(hotel.hotelvillaId)}" icon="ui-icon-radio-on" type="submit" value="Provide Service">
<f:param name="roomserviceid" value="#{roomServiceManagerBean.roomServiceId}" />
</p:commandButton>
</c:otherwise>
However, it fails and the both buttons appear. How is this caused and how can I solve it?
The <c:if> will in this construct fail if #{hotel} is not available during view build time but only during view render time (e.g. because it's definied as <p:dataTable var>). The <c:otherwise> is completely displaced here. It belongs to a <c:choose><c:when>. Technically, you should be using <c:if> instead with a negation in the test. Or, you should be replacing the first <c:if> by a <c:when> and wrap the whole thing in <c:choose>. However that would still fail if #{hotel} is not available during view build time.
Just use JSF component's rendered attribute instead.
<p:commandButton ... rendered="#{roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
...
</p:commandButton>
<p:commandButton ... rendered="#{not roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
...
</p:commandButton>
See also:
EL expression in c:choose, can't get it working
JSTL in JSF2 Facelets... makes sense?

Struts2 : Double Iteration over Parameters does not work?

Hello I am a young Software developer,
and I struggled the last 5 days with my code.
Here is my code in JSP:
<s:iterator value="getListeDanach()" status="stat">
<li>
<s:url id="URL_ListeDanach" action="uebersicht_umblaettern">
<s:param name="angeklickteSeitenzahl" value="getListeDanach()[#stat.index]" />
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}" cssClass="naviTab">
<s:property value="getListeDanach()[#stat.index]" />
</s:a>
</li>
</s:iterator>
My problem is, the first Iteration works great but the 2nd Iteration works half. In the 2nd case the property works, but the param doesn´t work! Al Variables are available. If i take the param Tag of the 2nd Iteration and place it in the first, it works great! But that isn´t what I want.
This is not an answer.
Here's the JSP, cleaned up, and using more S2 functionality. It was impossible to read the original.
<s:iterator value="listeDanach" status="stat" var="outerItem">
<li>
<s:url id="URL_ListeDanach" action="child">
<s:param name="angeklickteSeitenzahl" value="outerItem" />
<%-- What are you trying to do here? --%>
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<%-- What are you trying to do here? There's nothing to iterate over. --%>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}">
<s:property value="outerItem" />
</s:a>
</li>
</s:iterator>
In the bean i have a loop with 3 "rounds", in the 2nd iterator i use the var=counter to iterate three times over the property and over the dynamic parameter.
The property shows in HTMl in every loop of the first iterator this result : 0 1 2;
This is how it should work(the property is just there, to test the functionality of the 2nd itarator.)
But in the 2nd case, the parameter-Tag is fully ignored or something like that. For those who want to know the logic behind the code. It is a side navigation bar. listeDav or = list behind the actual number, and listeDanach= list after the actual number. 1 2 3 4 [5] 6 7 8 9.... When the param Tag in the 2nd itarator functions well, I would make the param tag dynamically with the iterated index.
SO in short, what I want is: Every time the first Iterator has his loop, I want to create dynamic parameters. This parameters are defined in the JSP before and are fully supported! I want to use the index "#stat1.index" to make it work.
Something like this :
s:param name="%{optionaleParamName[#stat1.index]}" value="#optionaleParamValue[#stat1.index]" />.....
i have already defined the String behind "#optionaleParamValue[0], behind #optionaleParamValue[1] and behind #optionaleParamValue[2] and soo on... ... and all this is for reusing the actual JSP.
As you can mention, a side navigation bar, can be used in many other cases in the programm.
Greetings

Composite component inside a ui:repeat, causes actionSource to not fire

OK, I'm guessing that this might be a bug but I'm not totally sure. Can anyone see what I'm doing wrong?
Here's the situation. I have a composite component that seems to work perfectly when it's alone on the page. The problem comes when the component is nested inside of a ui:repeat. Here are the two places I am calling the component (just for a test):
<q:case aCase="#{userSummaryBackingBean.userCases.get(0)}">
<f:setPropertyActionListener for="clickEvent" target="#{userSummaryBackingBean.value}" value="single"/>
<f:ajax execute="#this" render="#this :westPaneForm"/>
</q:case>
<ui:repeat value="#{userSummaryBackingBean.userCases}" var="aCase">
<li>
<q:case aCase="#{aCase}">
<f:setPropertyActionListener for="clickEvent" target="#{userSummaryBackingBean.value}" value="repeat"/>
<f:ajax execute="#this" render="#this :westPaneForm"/>
</q:case>
</li>
</ui:repeat>
and the component is defined as follows:
<cc:interface>
<cc:attribute name="aCase" type="com.autonomy.calltrack.data.dto.Case"/>
<cc:actionSource name="clickEvent" targets="caseCommandLink"/>
<cc:attribute name="action" targets="caseCommandLink" />
<cc:attribute name="actionSource" targets="caseCommandLink" />
<cc:clientBehavior name="click" event="action" targets="caseCommandLink" default="true"/>
</cc:interface>
<cc:implementation>
<h:commandLink id="caseCommandLink" styleClass="case ui-state-default" title="#{cc.clientId}">
--- more code ---
</h:commandLink>
</cc:implementation>
As you can see, the calls are exactly the same but the single item works and the items in the ui:repeat do not. Now, it makes sense to me that things will be problematic when the component itself has a ui:repeat. I mean, how would you retarget your actionSource, etc without knowing the ID of the items you need (which, inside of the repeat, is basically impossible).
HOWEVER, when I put the ui:repeat outside of the component I can't wrap my head around why things wouldn't work. The REALLY odd thing is that the f:ajax seems to work totally fine. VERY odd.
I've tried prefixing the targets attributes with :#{cc.clientId} and that doesn't help either (even though the "path" to the components is correct). Am I doing something wrong? Does anyone have a solution?
Alright, something must be wrong because even this doesn't work:
<ui:repeat value="#{userSummaryBackingBean.userCases}" var="aCase">
<h:commandLink id="caseCommandLink" value="test" styleClass="case ui-state-default">
<f:setPropertyActionListener target="#{userSummaryBackingBean.value}" value="REPEAT"/>
<f:ajax execute="#this" render="#this :westPaneForm"/>
</h:commandLink>
</ui:repeat>
What the heck could I be missing?
My page was using view params but they weren't a child of f:view so they were causing problems.
When I moved them everything started to work normally. I'm not sure why but it seems that things break when you have something like the following outside of an f:view:
<f:metadata>
<f:viewParam name="caseId" value="#{caseBackingBean.viewingCaseNumber}" />
</f:metadata>
<f:event type="preRenderView" listener="#{caseBackingBean.loadCase}" />
Thanks for the help BalusC.

Highlighting entire table row if validation fails

I am working on JSF2.0 and Richfaces. I am having an requirement where I need to change the style of the form field(entire row) if the validation fails. I am using the following code to display field label and text box.
<h:panelGrid columns="2">
<h:outputLabel value="#{uit.firstname}:">
<span class="required"><strong>*</strong> </span>
</h:outputLabel>
<h:inputText value="#{editUserProfileBean.firstName}" type="text"
id="firstname" styleClass="basicFormTextBox" size="30"
required="true" requiredMessage="#{uitkem.valueRequired}"
validatorMessage="#{prod.firstNameValidator}">
<f:validateLength maximum="#{prodConf.MaxLengthForFirstName}"
minimum="#{prodConf.MinLengthForFirstName}" />
<f:validator validatorId="trimSpaces" />
</h:inputText>
</h:panelGrid>
Suppose if the validation fails, I need to hightlight the row(both label and textbox).
I can use the following code to hightlight the textbox if the validation fails. But I want to hightlight the entire row, that is not possible using the following code.
<h:inputText value="#{editUserProfileBean.firstName}" required="true" styleClass="#{not component.valid ? 'newStyleClass' : ''}" />
Can anyone help me on this?
Thanks in advance.
You could just write a tiny JS script with jQuery, or use <rich:jQuery> for that.

Resources