JSF specifying it as parameter in the URL JSF - jsf

I have this JSF page with a form which contains fields "company". I am trying to pre-populate the company fields with the URL.
So, once the following link is clicked
localhost/accounts/index.jsf?company=test
users should see the form with company field filled with value "test".
Here is my JSF file
<p><h:inputText id="company" name="company" value="#{Bean.company}" required="true"
requiredMessage="Please enter your company" /></p>
I have tried to use f:viewParam by followed this tutorial (8. page parameters), but still could not get it working.
http://java.dzone.com/articles/how-do-10-common-tasks-jsf-20
Does anyone know how to fix this?
Thanks!

To capture param value, you should use <f:metadata>
<f:metadata>
<f:viewParam name="company" value="#{Bean.company}" />
</f:metadata>
<p>
<h:inputText id="company" name="company"
value="#{Bean.company}" required="true"
requiredMessage="Please enter your company" />
</p>

Related

synchronize E mail automatically with database

I wanted to know from you whether it is possible to check an email entry, if it exists already in the database and to display a message .
This validation should work dynamically without a button.
I work with jsf 2.1 and prime faces 3.5
I hope you understand what I mean and thank you for your help.
You need to implement a custom validator. Full explanation in this guide
Snippet code:
<h:inputText id="email" value="#{bean.email}">
<f:validator validatorId="myEmailValidator" />
<f:ajax event="keyup" render="emailError" />
</h:inputText>
<h:message id="emailError" for="email" />

primefaces input with tooltip button

I would like to create input-field-attached-tooltip-button (I don't quite know what to call it). Something like this:
When you hoover over the question mark icon you see the help text.
Justification: The little icon makes it blatantly obvious that there's help attached to the field. You can see that help is indeed available without even moving the mouse pointer to the field. Gives the user this warm fuzzy feeling.
I cannot figure out how to do this in PrimeFaces.
I do not know which framework has produced the example above but I'm pretty sure it is not PrimeFaces.
I'm well aware of <p:tooltip> but I don't think it can do this.
So here's what I've come up with in PrimeFaces:
(my screencapture does not show the mouse pointer)
I pretty much took BalusC's suggestions and created a JSF custom component. Here's what my custom component look like:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="helptext"/>
<composite:attribute name="show" />
</composite:interface>
<composite:implementation>
<p:button id="help" style="width: 15px; height: 15px" icon="ui-icon-help" rendered="#{cc.attrs.show}"/>
<p:overlayPanel for="help" showEvent="mouseover" hideEvent="mouseout" hideEffect="fade" >
<p:panel>
#{cc.attrs.helptext}
</p:panel>
</p:overlayPanel>
</composite:implementation>
</html>
and the xhtml that has produced the above screenshot looks like this:
<h:form>
<p:panelGrid columns="3" styleClass="noBorders">
<p:outputLabel for="mydate" value="Date" />
<p:calendar id="mydate" pattern="yyyy/MM/dd" />
<customC:fieldhelp helptext="Help text for date field." show="true" />
<p:outputLabel for="myboolean" value="Send now ?" />
<p:selectBooleanCheckbox id="myboolean" />
<customC:fieldhelp helptext="Help text for boolean field." show="false"/>
<p:outputLabel for="mypwd" value="Your password" />
<p:password id="mypwd" size="30" />
<customC:fieldhelp helptext="Help text for password field." show="true"/>
</p:panelGrid>
</h:form>
Seems to work fine. As you can see I had to reduce the size of the icon otherwise it would be way too big.
Some notes:
The composite doesn't wrap both the help icon and the field. It only
wraps the help icon. This has the downside that you'll need a three
column layout and that the help icon is not displayed right next to
the field. Instead it is displayed in its own column. Not exactly what I wanted.
The help text is conveyed in an attribute. This has the downside that
it cannot contain any HTML markup regardless of the fact that the
destination, the <p:panel>, would happily accept HTML markup.
You can leave out the help icon using the show attribute, however -
because of the three column layout - the <customC:fieldhelp> always
need to exist for each field. This is very clumsy but is again a result
of my lack of knowledge. :-(
I'm sure it can be improved upon if one knew a little more JSF/PrimeFaces than myself.
EDIT
If you want the icon right next to the input field (rather than in a column of its own) then you'll need the <h:panelGroup> tag. You can wrap a group of elements in this tag and <p:panelGrid> (or its sibling : <h:panelGrid>) will then count it as a single element, i.e. occupies one cell only. Something like this:
<h:form>
<p:panelGrid columns="2" styleClass="noBorders">
<p:outputLabel for="mydate" value="Date" />
<h:panelGroup>
<p:calendar id="mydate" pattern="yyyy/MM/dd" />
<customC:fieldhelp helptext="Help text for date field." show="true" />
</h:panelGroup>
<p:outputLabel for="myboolean" value="Send now ?" />
<h:panelGroup>
<p:selectBooleanCheckbox id="myboolean" />
<customC:fieldhelp helptext="Help text for boolean field." show="false"/>
</h:panelGroup>
<p:outputLabel for="mypwd" value="Your password" />
<h:panelGroup>
<p:password id="mypwd" size="30" />
<customC:fieldhelp helptext="Help text for password field." show="true"/>
</h:panelGroup>
</p:panelGrid>
</h:form>
In addition I had to do some CSS tweaking with vertical-align: middle to get the icon vertically aligned with the rest.

Is it possible to forward to Siteminder login.fcc from a JSF action?

I have a "custom" (JSF) login page for my application. My company uses Siteminder for authentication. So, in order to login to Siteminder with a "custom" page, I have to have a form that posts to /login.fcc with the appropriate form fields (username/password and a couple of Siteminder specific hidden fields).
What I am trying to do is to continue to leverage the form validations and such of JSF and still post to login.fcc.
Here is a stripped down version of my custom form:
<rich:messages />
<h:form id="adminLogin" prependId="false">
<h:panelGrid columns="2" styleClass="center">
<h:outputLabel value="HUB USERNAME:" />
<h:inputText id="USER" label="Username" size="12" required="true" />
<h:outputLabel value="HUB PASSWORD:" />
<h:inputSecret id="PASSWORD" label="Password" size="12" required="true" />
<input type="hidden" name="target" value="#{loginController.smTarget}" />
<input type="hidden" name="smagentname" value="#{loginController.smAgentName}" />
</h:panelGrid>
<h:panelGroup layout="block" styleClass="max-width button-panel">
<a4j:commandButton id="adminLoginBtn" value="LOGIN"
styleClass="login-btn"
action="#{loginController.managerLogin}" />
</h:panelGroup>
</h:form>
Note that I have used standard hidden inputs for the target and smagentname fields. This is because I have no need to have both setters and getters in my controller for these fields. I simply want the controller to provide the values when the page renders.
My controller method looks like this:
public void managerLogin()
{
LoginController.LOG.debug("manager is logging in...");
LoginController.LOG.debug("user: " + getRequestParameterValue("USER"));
LoginController.LOG.debug("smagent: " + getRequestParameterValue("smagentname"));
LoginController.LOG.debug("target: " + getRequestParameterValue("target"));
forwardTo(LoginController.SM_LOGIN_FCC);
}
You can see that I am logging the values from all fields except the password field and they all print correctly in my log...so I know I am getting the values. My "forwardTo()" method simply gets the ExternalContext and calls dispatch() on it...passing in the path to the login.fcc page (which is "/forms/login.fcc").
When I input values in the form and click the login button, I see my values in the console but I get a 404 message in my browser.
I can manually put the "/forms/login.fcc" page in the address line and I have no problems with the GET rendering the login.fcc page. But I, of course, don't want to render the login.fcc (GET) I want to peform the Siteminder login (via POST).
It is my understanding that all request parameters are made available through a forward process I was expecting this to work.
I can take this same form and un-JSF-ize it...and make the login button simply submit the form (whose action would be the /forms/login.fcc via POST and it works fine too, but then I loose the benefits of the form/field validations that JSF provides.
Any ideas?
Use the following process:
Follow the SiteMinder redirect
Send your credentials (username/password)
Follow the SiteMinder redirect again
Send the SiteMinder cookies
Here is an article which explains the process in detail:
How to Connect to a SiteMinder Protected Resource

How to parameterize an a4j:commandLink properly?

I have the following problem: I need to redirect from a "list page" to a details page, but I need the id from the list. "record" in this example is the var attribute of a rich:dataTable. First of all I thought about this:
<a4j:commandLink id="detailsLink" value="show details" execute="#this" action="/customerDetails?faces-redirect=true&cusid=#{record.id}" />
But this is invalid syntax, so I tried something like this:
<a4j:commandLink id="detailsLink" value="show details" execute="#this" action="/customerDetails?faces-redirect=true">
<f:attribute name="cusid" value="#{record.id}"/>
</a4j:commandLink>
(I even tried f:param)
On the target page I tried to receive the value with...
<f:metadata>
<f:viewParam required="false" name="cusid" value="#{customerBean.editCustomer}"/>
</f:metadata>
Basically f:metadata works, because when I try it with the following hard coded parameter, I receive its value:
<a4j:commandLink id="detailsLink" value="show details" execute="#this" action="/customerDetails?faces-redirect=true&cusid=120" />
I found a solution, but I'm not sure if this is the right way:
In customerBean I make the following:
public String editCustomer(long customerId)
{
edit(customerId);
return "/customerDetails?faces-redirect=true";
}
But I don't think that this is the usual way to send and receive parameters with Rich Faces. Is there maybe a better solution?
The <a4j:commandLink> sends an ajax POST request while you need a normal GET request. Use <h:link> instead.
<h:link value="show details" outcome="/customerDetails?cusid=120" />
Here's the best way to do it. The example provided passes 2 parameters. You can have more than one. Just use the assignTo attribute and the value attribute. Hope this answers your question.
<a4j:commandLink action="#{myBackingBean.myAction}">
<a4j:param name="jobIdParam" value="#{job.jobNumber}"
assignTo="#{myBackingBean.jobId}" />
<a4j:param name="isDisplayedParam" value="true"
assignTo="#{myBackingBean.displayed}"/>
</a4j:commandLink>

How to access html components in JSF EL?

I want some code in facelet (jsf 2.0) to work:
<h:inputText id="q" />
<h:button outcome="/search.xhtml?q=#{q.value}" />
but when I press the button, search page opens without any parameters.
I think, my EL expression is wrong. Is it possible to access inputText value from EL expression? If not, how can I achieve the same functionality?
I've finished with using plain html form:
<form action="faces/search.xhtml" method="get">
<input type="text" name="query" />
<input type="submit" value="Find" />
</form>
In search.xhtml I have view param to get a value of query string:
<f:metadata>
<f:viewParam name="query" />
</f:metadata>
This solution has the problem - "faces/search.xhtml" is hardcoded. Also, when I place this form in search.xhtml and perform several searches I have something like this in browser url:
"http://localhost:8080/Application/faces/faces/faces/search.xhtml"
I think this problem can be solved with PrettyFaces (TODO :)
this is a late answer but I guess many people finding this post will have the same doubt whether EL can access html component and how it can:
Yes, EL can access JSF component which are implicit JSF object.
However, up to now I have only seen and used the example of component.valid:
<h:inputText id="streetNumber" value="#{property.streetNumber}" title="streetNumber" required="true" requiredMessage="Please enter the street number!" validatorMessage="Please enter a street number between 1 and 1000" converterMessage="Please enter a number" pt:placeholder="900">
<f:convertNumber integerOnly="true"/>
<f:validateLongRange minimum="1" maximum="1000" />
</h:inputText>
<h:message for="streetNumber" class="#{!streetNumber.valid ? 'label label-warning' : 'none'}" />
So, I suggest you(Dmitry) change the code to
<h:button outcome="#{"/search.xhtml?q=" + q.value}" />
Please let me know whether it works because I am also curious and anyone who faces the similar problem can follow my example and please let me know the result.

Resources