I am trying to access the value of the expression $result.id and use that value and pass it in the bean action commitBtn. how do I go about doing this.
<c:forEach items="${bean.results}" var="result">
<fieldset>
<legend>
<b>Title:</b>
${result.id}
<c:set var="Id" value="${result.id}" />
<!-- this Id doesn't show as well, why -->
<h:outputText value="#{Id}" binding="#{bean.Id_lbl}" id="iD_lbl" />
<h:commandButton value="Commit" binding="#{bean.commitBtn}"
id="commitBtn" action="#{bean.commitBtn}" />
</legend>
...
Use the <f:param> tag to pass the value to the action. Within the action you will have to get the param value from the requestMap
something like:
<h:commandButton blah blah blah>
<f:param name="resultId" value="${result.id}" />
</h:commandButton>
then in action code:
resultId = FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("resultId");
The f:param as some may suggest does not work in a h:commandButton in JSF 1.x. Use f:setPropertyActionListener instead:
<h:commandButton value="Commit" binding="#{bean.commitBtn}" id="commitBtn" action="#{bean.commitBtn}">
<f:setPropertyActionListener target="#{bean.resultId}" value="#{result.id}" />
</h:commandButton>
Also see this article for more hints.
Related
I have concluded that the function cannot be called if I place the h:form within the foreach loop.
If I place the form outside and the button outside the foreach it will call the correct function. But how do I get desired result with one button for each row where i can pass the input text as a new value to the function?
Note that the getSeriesForPlayerIInfo works as it should
<c:forEach var="list" items="#{serviceSeries.getSeriesForPlayerInfo(club.name, player.stringID, aSerie, calendarBean)}">
<h:form>
<h:outputText value=" #{list[0] }" />
<h:outputText value=" #{list[1] }" />
<h:outputText value="#{serie.getSerieDateString(list[2]) }" />
<h:outputText value="#{list[3]}"/>
<h:outputText value=" #{list[4] }" />
<h:outputText value=" #{list[5] }" />
<h:outputText value=" #{list[6] }" />
<h:inputText value=" #{list[7] }" />
<h:outputText value=" #{list[8] }" />
<h:commandButton action="#{serviceSeries.PlayerSerie}" value="Uppdatera">
</h:commandButton>
</h:form>
</c:forEach>
In this construct, the <c:forEach> runs during view build time producing the JSF component tree. In effects, you end up with physically multiple <h:inputText> components in JSF component tree, each with the same binding:
<h:inputText ... binding="#{serieScore}" />
<h:inputText ... binding="#{serieScore}" />
<h:inputText ... binding="#{serieScore}" />
...
This is not right! You're binding physically different components to one and same bean property, hereby causing a messed up view state shared between all those components.
Get rid of binding altogether. It's the wrong tool for the job anyway. You should be using the value attribute the right way in order to get/set a value.
<h:inputText value="#{list[7]}" />
Supply if necessary a Converter if it's not a standard type like String, Number, Boolean, etc.
See also:
JSTL in JSF2 Facelets... makes sense?
What is component binding in JSF? When it is preferred to be used?
I want to pass the "title" parameter in the following listing dynamicaly to another jsf Facelet, depending on the selection of the selectOneMenu. My first approach looks like this:
<h:form id="form">
<p:selectOneMenu value="#{exerciseEditorBean.selectedExerciseType}" >
<f:selectItem itemLabel="Multiple Choice Exercise" itemValue="MultipleChoiceExercise" />
<f:selectItem itemLabel="Vocabulary Test" itemValue="VocabularyTest" />
</p:selectOneMenu>
<h:outputText value="Enter Title of your Exercise: " />
<h:inputText id="title" value="#{exerciseEditorBean.exerciseTitle}" />
<h:commandButton value="Next" action="#{exerciseEditorBean.openEditor()}" />
</h:form>
The ExerciseEditorBean is ViewScoped.
The openEditor() function then decides by the selectedExerciseType attribute which Facelet to show next and returns something like "multipleChoiceEditor.xhtml".
How can I now pass the titel attribute to this Facelet?
I now use the f:ViewParam in the target servelet, which works well except, that "multipleChoiceEditor.xhtml?includeViewParams=true" does not work, but this is another issue. Thanks for the discussion!
I would like to pass the EL one JSF page to Facelets template. Facelets template just understand EL value as string value. How can I pass EL String to Faceltes Template?
page1.xthml
<ui:include ..../>
<ui:param actionBeanMethod="#{EmployeeActionBean.deleteEmplayee(emp)}>
page2.xthml
<ui:include ..../>
<ui:param actionBeanMethod="#{DepartmentActionBean.deleteDepartment(dep)}>
In comfirmationTemplate.xml
<a4j:commandLink onclick="#{rich:component('confirmation')}.show();return false">
<h:graphicImage value="/img/delete.png" />
</a4j:commandLink>
<a4j:jsFunction name="submit" action="#{actionBeanMethod}"/>
<rich:popupPanel id="confirmation" width="250" height="150">
<f:facet name="header">Confirmation</f:facet>
<h:panelGrid>
<h:panelGrid columns="2">
<h:graphicImage value="/img/alert.png" />
<h:outputText value="Are you sure?" style="FONT-SIZE: large;" />
</h:panelGrid>
<h:panelGroup>
<input type="button" value="OK" onclick="#{rich:component('confirmation')}.hide();submit();return false" />
<input type="button" value="Cancel" onclick="#{rich:component('confirmation')}.hide();return false" />
</h:panelGroup>
</h:panelGrid>
</rich:popupPanel>
I would like to change action of a4j:jsFuction dynamically.
When page1.xhtm is call,
<a4j:jsFunction name="submit" action="#{EmployeeActionBean.deleteEmplayee(emp)"/>
When page2.xhtm is call,
<a4j:jsFunction name="submit" action="#{DepartmentActionBean.deleteDepartment(dep)"/>
Can it possible?
You can't pass method expressions as <ui:param> value. It accepts only value expressions.
You basically need to create a custom taghandler which re-interprets the value expression as a method expression. From the current open source JSF component/utility libraries, the OmniFaces <o:methodParam> is the only one which does exactly that.
<o:methodParam name="methodParam" value="#{actionBeanMethod}" />
<a4j:jsFunction name="submit" action="#{methodParam}" />
You only need to register the Facelets include as a Facelets tag file and use it as
<my:confirmationTemplate actionBeanMethod="#{EmployeeActionBean.deleteEmplayee(emp)}" />
An alternative is to use a composite component instead.
I am using jsf2 and richfaces 4.X how can i get the richfaces enabled input field's current value ? equal to getElementById('field_name').vlue
I tried some of the methods named like findcomponent.value and element.value but they give me old value which was while page was loaded b/c findcomponent and element method returns server side UI.components instead client side components ?
I guess you are to use rich:clientId function to render a correct identifier into your java script.
For example:
var myElement = document.getElementById("#{rich:clientId('field_name')}");
See also RichFaces rich:clientId within facelets
If you check the HTML generated, you could see that every JSF / RF element has his id like :. For example:
<h:form id="frmSample">
<h:inputText id="txtSample" value="#{mybean.someTextValue}" />
</h:form>
The HTML generated will be:
<form method="POST">
<input type="text" id="frmSample:txtSample" />
</form>
So, in JavaScript you could reference the element by this id.
var txtValue = document.getElementById('frmSample:txtSample').value;
Also, for the RF composite HTML components like rich:tab or rich:calendar you could use the component ID generated by HTML but I'll recommend "#{rich:clientId('field_name')}" as #DRCB has explained in his post.
To get the richfaces components values use the rich:component and the getValue() methods.
To get the jsf library components values use the rich:element and the value JS methods.
Example:
<h:form>
<h:inputText id="textField" value="#{bean.value1}" />
<a4j:commandButton value="displayTextField" action="doSomething()"
onclick="alert('v:'+ #{rich:element('textField')}.value); return true;" />
<rich:inplaceInput id="richField" value="#{bean.value1}" />
<a4j:commandButton value="displayRichField" action="doSomething()"
onclick="alert('v:'+ #{rich:component('richField')}.getValue()); return true;" />
</h:form>
They both work even in a rich:dataTable, without adding the id of the datatable or something. Richfaces is quite smart and find the ids of the 'current row'
example:
<h:form>
<rich:dataTable id="mydatatable" value="bean.findValues()" var="myvar">
<rich:column>
<h:inputText id="textField" value="#{myvar.text1}" />
<a4j:commandButton value="displayTextField" action="doSomething()"
onclick="alert('v:'+ #{rich:element('textField')}.value); return true;" />
</rich:column>
<rich:column>
<rich:inplaceInput id="richField" value="#{myvar.text2}" />
<a4j:commandButton value="displayRichField" action="doSomething()"
onclick="alert('v:'+ #{rich:component('richField')}.getValue()); return true;" />
</rich:column>
</rich:dataTable>
</h:form>
I want to find out the previous page from where the current page is called. Based on the previous page I want to enable or disable a particular component. Can anybody help me in this regard.
Depends on the concrete functional requirement which isn't entirely clear from your question. You could pass an unique request parameter. E.g. when it concerns a GET link:
<h:link value="Next" outcome="next">
<f:param name="foo" value="bar" />
</h:link>
or a POST link:
<h:form>
<h:commandLink value="Next" action="next">
<f:param name="foo" value="bar" />
</h:commandLink>
</h:form>
with in next.xhtml
<h:someComponent rendered="#{param.foo == 'bar'}">
...
</h:someComponent>
or if you don't care about the param's value:
<h:someComponent rendered="#{not empty param.foo}">
...
</h:someComponent>
An alternative which can be much better if you don't want to allow the enduser being able to manipulate the request is to set a bean property during a POST action and then return to the next view:
<h:form>
<h:commandLink value="Next" action="#{bean.next}" />
</h:form>
with e.g.
public String next() {
foo = "bar";
return "next";
}
and in next.xhtml
<h:someComponent rendered="#{bean.foo == 'bar'}">
...
</h:someComponent>