Assign dynamic ids to hidden fields when iterating over a collection - java

Is there a way to assign dynamic ids to h:inputHidden components?
EDIT1
I am trying to assign the ids inside a ui:repeat tag when iterating over a collection of elements.

It is not possible to set the ID based on the iteration value of an <ui:repeat>. But you don't need it anyway. They will by default already get dynamic and unique IDs based on the iteration index.
E.g.
<h:form id="form">
<ui:repeat value="#{bean.list}" var="item">
<h:inputHidden id="hidden" value="#{item.value}" />
</ui:repeat>
</h:form>
will generate this HTML during view render time
<form id="form" name="form">
<input type="hidden" id="form:0:hidden" name="form:0:hidden" value="item1value" />
<input type="hidden" id="form:1:hidden" name="form:1:hidden" value="item2value" />
<input type="hidden" id="form:2:hidden" name="form:2:hidden" value="item3value" />
</form>
If you want to manually control the ID, you'd need to use <c:forEach> instead, because <ui:repeat> doesn't generate multiple JSF components, but lets its children (which is a single <h:inputHidden> in the above example) generate HTML multiple times. The <c:forEach> will generate multiple JSF components which then each generate HTML only once (so you effectively end up with multiple <h:inputHidden> components in JSF component tree).
E.g.
<h:form id="form">
<c:forEach items="#{bean.list}" var="item">
<h:inputHidden id="#{item.id}" value="#{item.value}" />
</c:forEach>
</h:form>
which will basically generate this JSF component tree during view build time
<h:form id="form">
<h:inputHidden id="item1id" value="#{bean.list[0].value}" />
<h:inputHidden id="item2id" value="#{bean.list[1].value}" />
<h:inputHidden id="item3id" value="#{bean.list[2].value}" />
</h:form>
which in turn will generate this HTML during view render time
<form id="form" name="form">
<input type="hidden" id="form:item1id" name="form:item1id" value="item1value" />
<input type="hidden" id="form:item2id" name="form:item2id" value="item2value" />
<input type="hidden" id="form:item3id" name="form:item3id" value="item3value" />
</form>
See also:
JSTL in JSF2 Facelets... makes sense?

They get assigned a dynamic ID by default. You can also specify id="#{..} to customize it.

You can dynamically add any random number also (id="#{}"),but
add functionally related ids to hidden components, that will be helpful
for example if it is employee form ,you may add empid to it.

Related

Reset form values onload in bootsfaces

I need to reset the form fields and clear the form values onload.
However I used -
public void refresh() {
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
ViewHandler viewHandler = application.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(context, context
.getViewRoot().getViewId());
context.setViewRoot(viewRoot);
context.renderResponse(); //Optional
}
The above code is working but when I am entering values in fields using in panelgroup , its not working.
<h:panelGroup>
<ui:repeat value="#{formDynAttrbBean.vals}" var="values">
<b:panelGrid columns="6" cellspacing="4">
<b:inputText id="label" value="">
</b:inputText>
<b:selectOneMenu value="">
<f:selectItem itemLabel="SelectType" itemValue="" />
<f:selectItems value="" />
</b:selectOneMenu>
<b:selectBooleanCheckbox value="" />
<b:commandButton actionListener="#{form.types}" />
</b:panelGrid>
</ui:repeat>
<b:commandButton tooltip="Add new attribute" tooltip-position="bottom"
iconAwesome="plus" actionListener="#{form.add}" update="#form" />
<tr />
<b:commandButton value="Save" ajax="true"
actionListener="#{form.submit}" update="#form" />
<tr />
<b:commandButton type="reset" action="#{formController.refresh}"
value="Reset">
</b:commandButton>
</h:panelGroup>
Refresh function is not working in this case when I am entering values in panelgroup and in repeating fields.
I am just a beginner to bootsfaces.Please suggest me an approach.Thankyou in advance.
There are ways to clear the input fields of the form. For example, JSF 2.2 has the resetValues="true" attribute (see the article of Michael Kurz (probably not implemented by BootsFaces). Another option is to use <b:commandButton type="reset">.
But I wonder why you want to reset the values on the client side? You have to clear them because they display some "dirty" (i.e. modified) backend bean. IMHO the better approach is to create a new, empty backend bean.

Updating non-JSF form

I have a situation where I am suppose to submit a user's payment details to my payment gateway partner.
For this I have a separate non JSF form with hidden values as parameters to be passed.
<form id="hiddenForm" action="#{checkoutBean.payuAction}" method="post" name="payuForm">
<p:outputPanel id="hiddenPanel">
<h:inputHidden id="hash" value="#{checkoutBean.hash}"/>
<h:inputHidden id="txnid" value="#{checkoutBean.txnid}"/>
<h:inputHidden id="amount" value="#{checkoutBean.totoalAmount}"/>
<h:inputHidden id="firstname" value="#{checkoutBean.firstname}"/>
<h:inputHidden id="email" value="#{checkoutBean.email}"/>
<h:inputHidden id="phone" value="#{checkoutBean.phone}"/>
<h:inputHidden id="productinfo" value="#{checkoutBean.productInfo}"/>
<h:inputHidden id="surl" value="#{checkoutBean.sURL}" />
<h:inputHidden id="furl" value="#{checkoutBean.fURL}"/>
</p:outputPanel>
</form>
The above hidden values are generated using ajax at the last step in a wizard (primefaces). The ajax call is happening but I am unable to update the non JSF form. The values are null.
<p:commandButton value="Confirm" actionListener="#{checkoutBean.initPayu}" onsuccess="submitPayuForm()" update=":hiddenForm:hiddenPanel" />
The above button is in a <h:form>.
Now i believe that it is not possible to update non JSF components from JSF components. But I just can't figure out how to tackle this scenario.
Any ideas on how I can tackle this problem?
Using : JSF 2 , Primefaces 3.3
Thanks a ton in advance.
Since your form is not a JSF component you don't need to append its id to ids of your JSF components (:hiddenForm:hiddenPanel).
In other words, try something like this:
<p:commandButton ... update="hiddenPanel" />

Possibility to get all form inputs from several layers of panels and forms

Kinda new to wicket and im wondering if its possible to use a AjaxButton in order to get form values from forms within the first form...
For example, using the "ajaxButton" below would it be possible to also get the value of "anothervalue" even though its not in the same form?
<form>
<input type="submit" wicket:id="ajaxButton" />
<input type="text" wicket:id="aValue" />
<panel>
<form>
<input type="text" wicket:id="anothervalue" />
</form>
</panel>
</form>
Wicket supports nested forms. When you submit the outer form, the inner one should also processed (validate params, update models).

JSF view calculations

I know that with JSF 2, facelets is the preferred view declaration language.
Is JSP to jsf deprecated?
Anyway, I need to create a special layout so I cannot use Datatable. Instead, I have 6 divs that I use as columns in which I drop a collection of Articles.
My problem is that I have a JSF composite component, that is injected with a Collection
A:
List<Article>
object.
The component then needs to divide the size of the collection into equal pieces for each column. Then set the appropiate offset and size for each
<ui:repeat></ui:repeat>
so i end up with this
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="featuredArticles" required="true" type="java.util.List;" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<div class="col">
<ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
<mycomps:article art="#{art}" />
</ui:repeat>
</div>
<div class="col">
<ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
<mycomps:article art="#{art}" />
</ui:repeat>
</div>
<div class="col">
<ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
<mycomps:article art="#{art}" />
</ui:repeat>
</div>
<div class="col">
...same here...
</div>
<div class="col">
...same here...
</div>
</cc:implementation>
So how do I calculate those offsets and sizes so that each columns iterates over a portion of the collection? Or maybe there's a better way?
You can get collection's size with fn:length and there are basic arithmetic operators in EL.
<ui:composition xmlns:fn="http://java.sun.com/jsp/jstl/core">
...
<ui:param name="size" value="#{fn:length(featuredArticles) / 6}" />
...
<ui:repeat size="#{size}">
...
</ui:composition>
Update: as to the rounding, that get tricky. In old JSP you could use JSTL <fmt:formatNumber> for this which can export to a var attribute instead of displaying it straight in the view.
<fmt:formatNumber var="size" value="${fn:length(featuredArticles) / 6}" pattern="0" />
But the JSTL fmt is not available in Facelets.
A hacky way would be to split the fractions using fn:substringBefore.
<ui:param name="size" value="#{fn:substringBefore(fn:length(featuredArticles) / 6, '.')}" />
But this always rounds down.
The best way would be to create a custom EL function. You can find an example in this answer. For JSF 2.0 you only need to replace the deprecated <param-name>facelets.LIBRARIES</param-name> by <param-name>javax.faces.FACELETS_LIBRARIES</param-name>. Finally you'll end up like as:
<ui:param name="size" value="#{x:roundUp(fn:length(featuredArticles) / 6)}" />
As a completely different alternative, you could also do this job in the constructor, init or getter of a managed bean.

Using spring:message to define form tag attribute in Spring web application

I'm developing a Java/Spring web application. The problem I'm currently facing is that I'd like to have message from message.resources shown as an attribute in an HTML.
<input type="submit" name="login" value="login" />
So instead of the hardcoded value "login" I need to the value of
<spring:message code="general.submit" /> as the value attribute of that input tag. As the pages are all xml, it's no option to nest tags like
<input type="submit" name="login" value="<spring:message code="general.submit" />" />
as it does not compile. I could, of course, read the value in the Java controller and use a JSTL variable to display the value, but I think it would be too hackish and complicated, especially for pages with large amount of submit buttons. Is there some kind of elegant way of accomplishing what I want to do?
Use <spring:message> to store the value in a var, then reference that var using EL, e.g.
<spring:message code="general.submit" var="submitText"/>
<input type="submit" name="login" value="${submitText}" />

Categories