Is it better to use JSF HTML or JSF Core namespaces with inputText when performing validation? - validation

I need to perform simple form validation using JSF. Looking at different online examples I see two patterns:
<h:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</h:inputText>
...or...
<f:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</f:inputText>
The only difference is the namespace of the inputText tag. Both solutions seem to work, but which one is "more correct" and what makes it a better choice?

There is no such thing f:inputText, inputText is a part of the http://java.sun.com/jsf/html namespace and should use the h prefix,
like this: xmlns:h="http://java.sun.com/jsf/html"
Where have you seen the <f:inputText anyway ?
The only way that you can use <f:inputText is only if you assign the f prefix your self instead of the h , like this: xmlns:f="http://java.sun.com/jsf/html" (but its really not a good idea) better stick to the original h

Related

Can a p:ajax component be used with p:outputLabel?

I have a p:dataTable with some columns that contain p:outputLabel components.
I want to add p:ajax functionality so that if the user clicks on the outputLabel, I can call a server-side process to do some stuff - in the simple case it would gather some information and display that in a dialog.
I'm looking for something like this
<p:outputLabel value="#{term.explanation}" >
<p:ajax event="click" listener="#{backingBean.myListener}" />
</p:outputLabel>
Obviously, this doesn't work, so I was wondering if someone might be able to give me a hint on how I can get something like this. The constraint is that the dataTable needs to keep the p:outputLabel components - just need some way to ajaxify them.
Thanks in advance.
Dave J.
Try to use
<p:commandLink actionListener="#{backingBean.myListener}"">
<p:outputLabel value="#{term.explanation}" > </p:outputLabel>
</p:commandLink>
You could use p:remoteCommand here. You can use it to execute backing bean methods using JavaScript. For example:
<p:remoteCommand name="onLabelClick"
actionListener="#{backingBean.myListener}"/>
<p:outputLabel value="#{term.explanation}"
onclick="onLabelClick()"/>
If you need to be able to know the specific p:outputLabel that was clicked you can add parameters to p:remoteCommand and use #{component.clientId} as the value.
See also:
Pass parameter to p:remoteCommand from JavaScript
What exactly is #{component} in EL?

f:validateRegex pattern for > 0.0

Before we start I want to inform you that I have done a lot of searching and have looked at all of the "Questions that may already have your answer", all over Google, and the <f:validateRegex pattern="" /> forms and the can not come across; how, by using <f:validateRegex pattern="" /> to make sure the entered value is greater than 0.0.
I am making a BMI calculator using PrimeFaces and I need this validation.
Here is the code where I am asking for the input:
<p:inputText id="height"
value="#{peopleData.height}"
size="6"
required="true">
<f:validateRegex pattern="[0-9+]+.+[0-9]+"/>
</p:inputText>
I was going to set pattern="[0-9+]+.+[1-9]+" but then the user would always need to have could not have a height of anything like *.0
I am absolutely lost with this and any suggestions would be awesome! Even if the solution would involve using something other than <f:validateRegex/>
Regex'es are mostly usefull for String validtion. As you request a Float/Double as input, try:
<f:validateDoubleRange minimum="0.1" maximum="..." />
This tag only allows digits and numeric values within the min-max-range.
See also: http://www.tutorialspoint.com/jsf/jsf_validatedoublerange_tag.htm

How to add a converter to parameterized messages in JSF 1.2?

I'm looking for a way to convert my params in a parameterized text inside a JSF 1.2 xhtml page.
Let's say I have this parameterized message in my messages.properties (please ignore the stupidity of this example):
EXAMPLE_TEXT=Right now {0} Euro is {1} US-Dollar.
And I would like the output to look like this:
Right now 1 Euro is 1.3658 US-Dollar.
Then my xhtml code looks like this:
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" />
<f:param value="#{backingBean.dollarValue}" />
</h:outputFormat>
I already have implemented a converter (let's say its ID is bigDecimalConverter) that converts numeric values to the particular formats.
My question now is: (How) can a converter be registered to the output's param values?
I imagine something like this (which unfortunately seems to not be possible):
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" converter="bigDecimalConverter" />
<f:param value="#{backingBean.dollarValue}" converter="bigDecimalConverter" />
</h:outputFormat>
Asking google I found this thread, but it's no sufficient answer to my specific problem.
Thanks a lot in advance for any help!
You can have exact the same output with outputText:
1) Change output to <h:outputText value="#{messages.TEXT1} #{backingBean.euroValue} #{messages.TEXT2} #{backingBean.dollarValue} #{messages.TEXT3}" />
2) Create method in backing bean which return full String and call it in output text.
(This answer was transformed from previous comment)

Containing div for JSF composite components

I have a number of composite components in my application, and from time to time, I need to reference those components as a whole. By default, composite components don't generate any additional mark up over their child components, so if you give a composite component an id attribute, it simply appends that id to the generated ids of the child components. I would like to reference the whole component by id (i.e. for use in jquery manipulation). JSF components do not allow EL in the id attribute, so to accomplish this, I have so far been wrapping my composite components in plain html divs as follows:
<ui:component ...
xmlns:cc="http://java.sun.com/jsf/composite"
...>
<cc:interface componentType="myComponent">
<cc:attribute name="process" type="java.lang.String" required="false" default="#form"/>
<cc:attribute name="update" type="java.lang.String" required="false" default="#form"/>
...
</cc:interface>
<cc:implementation>
<h:outputStylesheet name="myComponent.css" library="css/components" target="head"/>
<div id="#{cc.clientId}" class="myComponent #{cc.attrs.styleClass}">
... composite component implementation here ...
<p:dataTable id="note-history" styleClass="entity-notes-history" value="#{cc.notes}" var="note" paginator="true" paginatorAlwaysVisible="false"
paginatorPosition="bottom" rows="5" rendered="#{not empty cc.notes}" rowStyleClass="#{note.noteBaseType.word}">
...
<p:column rendered="#{note.noteBaseType == 'Warning' and not cc.attrs.displayOnly}" style="width: 8em;">
<p:commandButton actionListener="#{cc.cancelSeverity(note.id)}" process="#{cc.attrs.process}" update="#{cc.attrs.update} update="note-history" value="Reduce Severity"/>
</p:column>
</p:dataTable>
</div>
</cc:implementation>
And the component would be used as follows:
....
<ppa:myComponent id="myId" update="myId" />
....
Unfortunately, if I have any ajax (specifically p:ajax from primefaces) calls within the composite component, when I try to have them update the composite component by ID I get a "Cannot find component with identifier" exception. Is there any way to get JSF to auto generate a valid component and container for my composite components? I would really hate to have to generate two containing divs or spans (i.e. adding an h:panelGroup around the plain html div) just to get ajax working. Alternatively, is there a way to force JSF to allow dynamic ids on components?
Edit:
In response to BallusC's answer, I've edited the example code to be more clear in how the component is built and used, since given his answer it's entirely possible I'm just doing something that isn't allowed.
when I try to have them update the composite component by ID I get a "Cannot find component with identifier" exception
It's unfortunate that you didn't show how exactly you did that, because it should work just fine, provided that you used the composite's client ID in the update which is enclosed in the composite itself:
<f:ajax render=":#{cc.clientId}" />
Or, that you just used composite's own ID in the update which is performed outside the composite inside the same naming container parent:
<f:ajax render="myId" />
Most likely you did it the wrong way. E.g. you forgot the : prefix, or you used #{cc.id} instead of #{cc.clientId}, etc. The above has as far as I know always worked in all Mojarra 2.x versions released so far.
See also:
Referring composite component ID in f:ajax render
Is it possible to update non-JSF components (plain HTML) with JSF ajax?
Alternatively, is there a way to force JSF to allow dynamic ids on components?
You can just use EL in id attribute, provided that it's available during view build time and thus not only during view render time. See also JSTL in JSF2 Facelets... makes sense? for related background information.
Update as per your question update wherein you finally show how you tried to achieve it;
With
<ppa:myComponent id="myId" update="myId" />
and
<p:commandButton ... update="#{cc.attrs.update}" />
you're effectively ultimately doing a
<p:commandButton ... update="myId" />
which is basically looking for a component with ID myId inside the context of the composite itself! It would only have effect on e.g. a <h:outputText id="myID"> next to the <p:commandButton> inside the same <cc:implementation>.
The functional requirement is understood, but the solution is not so nice. Your closest bet is (ab)using #this and conditionally checking for that in update:
<ppa:myComponent id="myId" update="#this" />
with something like this
<p:commandButton ... update="#{cc.attrs.update == '#this' ? ':'.concat(cc.clientId) : cc.attrs.update}" />

JSF: Execute values of multiple forms

I want to submit (execute) values from multiple forms, not just the enclosing form. So, I want to be able to do something like this:
<h:form id="form1>
<h:inputText id="testinput1" value="#{testBean.input1}" />
</h:form>
<h:form id="form2>
<h:inputText id="testinput2" value="#{testBean.input2}" />
<h:commandButton value="Submit">
<f:ajax execute=":form1 :form2"/>
</h:commandButton>
</h:form>
How would you solve this?
What is <f:ajax execute="#all"> really supposed to do? It POSTs only the enclosing form
seems to be related, but addresses a slightly different problem and also does not solve it (or this).
Ajax or not, this is not possible with plain JSF/HTML. All input elements which needs to be processed really needs to go inside the same form.

Resources