How to access inputtext binding in jsf - jsf

I have the following problem and I could not find a solution anywhere.
I have the following code:
<h:inputText id="username" value="#{registrationBB.userName}" binding="#{userNameToConfirm}"/>
and later on:
<h:inputSecret id="confirmed-password" value="#{registrationBB.userPasswordConfirmed}">
<f:validator validatorId="usernameNotInPasswordValidator"/>
<f:attribute name="username" value="#{userNameToConfirm.value}" />
</h:inputSecret>
The inputText is bound to #{userNameToConfirm} (which is not a property in any backingbean) and later this binding is used in the password validator.
This all works well.
But, the form that these fields are on contains a "Reset" button, that should empty all fields on the form.
When the reset button is clicket, all properties in the registrationBB are emptied and the following code is called:
getViewRoot().getChildren().clear();
But, the username will never be empty, because it will always be set by the value in the "#userNameToConfirm" binding.
My question is: How can I access this binding and delete in the faces back-end?
We use jsf version 1.2.
regards,
arash

Let the reset button reload the page instead.
<h:commandButton value="Reset" onclick="location.reload(true)" />
Or by a <navigation-case> with a <redirect>.
<h:commandButton value="Reset" action="reloadPagename" />
with
<navigation-rule>
<navigation-case>
<from-outcome>reloadPagename</from-outcome>
<to-view-id>/pagename.jsf</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>

Related

JSF - rich:componentControl and a4j:jsFunction redirect issue

I have two questions in one. First off all, I don't understand how to use the rich:componentControl, I used an old code and this website: https://community.jboss.org/wiki/RichFaces33-4xMigrationGuideUnleashed but I still have this error whatever I try:
javax.servlet.ServletException: /admin.xhtml #191,30 Parent not an instance of ClientBehaviorHolder: javax.faces.component.html.HtmlOutputText#1ee71d84
javax.faces.webapp.FacesServlet.service(FacesServlet.java:521)
com.lesim.fst.service.FilterUTF8.doFilter(FilterUTF8.java:20)
There is my code:
<rich:column>
<h:outputText value="#{msg.la8}" id="actionId" >
<rich:componentControl event="onmouseover" target="menu" operation="show" />
</h:outputText>
<rich:contextMenu id="menu" mode="client">
<rich:menuItem label="#{msg.ll8}" onclick="functionEdit();" icon="/images/edit16.png" />
<rich:menuItem label="#{msg.ll9}" onclick="functionSuppr();" icon="/images/supprimer16.png" />
</rich:contextMenu>
<a4j:jsFunction name="functionEdit" action="#{administrateurBean.editAdministrateur()}" >
<a4j:param value="#{administrateur.idadministrateur}" name="idadministrateur" />
</a4j:jsFunction>
<a4j:jsFunction name="functionSuppr" action="#{administrateurBean.deleteAdministrateur()}">
<a4j:param value="#{administrateur.idadministrateur}" name="idadministrateur" />
</a4j:jsFunction>
</rich:column>
My goal is to show the rich:contextMenu when the h:outputText is onmouseover, and the rich:componentControl is needed because the onmouseover attribute don't work for rich:contextMenu (according to this website : https://issues.jboss.org/browse/RF-7497 )
You can also see that I don't directly use my Bean methodes, I put them in an a4j:jsFunction. It is because the action attribute don't work for rich:menuItem (according to this website: [Oh no... I can't give this one... I thought you were asking to give a maximum of my own search but apparently you don't want it so much... ] )
So I used his solution, but the redirection from the faces-config doesn't work
faces-config:
<navigation-rule>
<display-name>admin.xhtml</display-name>
<from-view-id>/admin.xhtml</from-view-id>
<navigation-case>
<from-action>#{administrateurBean.editAdministrateur}</from-action>
<from-outcome>edit</from-outcome>
<to-view-id>/editAdministrateur.xhtml</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-action>#{administrateurBean.deleteAdministrateur}</from-action>
<from-outcome>list</from-outcome>
<to-view-id>/admin.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
If someone sees a way to fix one of my problems, or both, you're welcome
Thanks,
Quentin
As the error message says h:outputText is not a behavior holder component (in particular this component does not trigger 'mouseover' event).
Refer to the documentation:
http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/
You can use for example h:outputLabel instead (note that event name is mouseover)
<h:outputLabel value="#{msg.la8}" id="actionId">
<rich:componentControl event="mouseover" target="menu" operation="show"/>
</h:outputLabel>
Or this way using onmouseover attribute:
<h:outputLabel value="#{msg.la8}" id="actionId"
onmouseover="${rich:component('menu')}.show()"/>

Richfaces 4 fileUpload redirect following uploadcomplete

I would like to redirect to another page following the upload of a single file. The source xhtml and destination xhtml are in the same directory. I have tried returning a String in the fileUploadListenerMethod:
public String loadFills(FileUploadEvent event) throws Exception {
return "treeOrderRequestStatus";
}
but this does not result in a redirect.
I have also tried accomplishing it via faces-config.xml:
<navigation-rule>
<from-view-id>/userpages/manualDataLoad.xhtml</from-view-id>
<navigation-case>
<from-action>#{manualDataLoader.loadFills}</from-action>
<to-view-id>/userpages/treeOrderRequestStatus.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/userpages/manualDataLoad.xhtml</from-view-id>
<navigation-case>
<from-outcome>treeOrderRequestStatus</from-outcome>
<to-view-id>/userpages/treeOrderRequestStatus.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
and neither of these worked.
Finally, I have tried executing a link triggered by the uploadcomplete event:
<rich:fileUpload fileUploadListener="#{manualDataLoader.loadFills}"
maxFilesQuantity="1" createTempFiles="false" uploadLabel="Import Fill Data"
addLabel="Select Fill File">
<a4j:ajax event="uploadcomplete" execute="redirect"/>
</rich:fileUpload>
<h:link id="redirect" outcome="treeOrderRequestStatus"/>
and still no dice. In each of these attempts I have tried multiple different combinations of specifying the destination page (e.g.: including the .xhtml extension, including the directory, ...) but have not yet succeeded.
What am I doing wrong?
The execute attribute tag is to define the components that will be sent in the request to the server. You can try clicking a link/button using the uploadcomplete JavaScript method from the <rich:fileUpload> tag component:
<rich:fileUpload fileUploadListener="#{manualDataLoader.loadFills}"
maxFilesQuantity="1" createTempFiles="false" uploadLabel="Import Fill Data"
addLabel="Select Fill File">
<a4j:ajax event="uploadcomplete" onbegin="document.getElementById('yourFormName:redirect').click();" />
</rich:fileUpload>
<h:commandButton id="redirect" action="treeOrderRequestStatus" style="display: none"
tabindex="-1" />
Also, it would be better if you erase those navigation rules in your faces-config.xml to keep the file clean of needless configurations.

How to invoke a command button without validating all required inputs?

I'm trying to make a redirection from index.xhtml to registerFirstTime.xhtml.
The code in the page index.xhtml is:
<h:form id="content" style="margin-top: 1em;">
<p:panel id="panel" header="LOGIN">
<p:messages id="msgs" globalOnly="true"/>
<h:panelGrid columns="3">
<h:outputLabel value="e-mail" />
<p:inputText id="name" required="true" value="#{UserBean.email}"
requiredMessage="Required: e-mail" display="icon">
</p:inputText>
<p:message id="msgName" for="name"/>
<h:outputLabel value="Password" />
<p:password id="password" required="true" value="#{UserBean.password}"
requiredMessage="Required: Password" display="icon" feedback="false">
</p:password>
<p:message id="msgPassword" for="password"/>
</h:panelGrid>
<h:panelGrid columns="2">
<p:commandButton value="Login" actionListener="#{UserBean.validate}" update="msgNombre, msgPassword, msgs"/>
<h:commandButton value="Register for the first time" action="register"/>
</h:panelGrid>
</p:panel>
</h:form>
While, the content of the redirection, in faces-config.xml, is:
<navigation-rule>
<from-view-id>index.xhtml</from-view-id>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>registerFirstTime.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Until the input name and password are filled, you can't do the redirect.
How I can get do the redirection of the record without taking into account the required fields? Thanks! =)
Add immediate="true" to the command button which should skip processing of all input field which do not have the immediate="true" attribute.
<h:commandButton value="Register for the first time" action="register" immediate="true" />
Unrelated to the concrete problem, please note that you're technically not sending a redirect here. This basically sends a forward, i.e. the URL in the browser address bar remains the URL of the login page. You need to add <redirect/> to the <navigation-case>. Also please note that navigation cases are an leftover of JSF 1.x. Since JSF 2.x you can perform "implicit navigation" whereby you just specify the view ID as outcome.
<h:commandButton value="Register for the first time" action="registerFirstTime?faces-redirect=true" immediate="true" />
This way you can get rid of the navigation case altogether.
#BalusC solution should do the trick, as always. But I'd like to point out a couple of things since you seem to be using Primefaces. Both are unrelated to the question but might help you out at some point.
First, you could use implicit navigation (introduced in JSF2). That way you wouldn't need to define all the navigation rules in your faces-config.xml file (I work on an old JSF 1.2 project and hate the need to define the navigation roles for everything). Here's how you'd do it:
<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true"/>
The faces-redirect parameter forces a redirect instead of a forward, in case you need that.
Also, say you want to properly process some values, but not all of them. In that case, you can use the process attribute of p:commandButton or p:ajax. For example
<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true" process="#this, name"/>
This would make JSF process only the button (#this) and the component with id="name" (your e-mail field). Again, it probably doesn't apply to this question, but it's something I use often.

cannot set managed-property parameter with a JSF expression value

I have a strange problem which I have been trying to fix for some time but am stuck in one place and don't quite understand what's going on here.
My index page looks like this:
<h:selectOneMenu id="selectMenu" value="#{indexBean.model.selected_id}" styleClass="indexItems">
<f:selectItems value="#{indexBean.myModelValues}" />
<a4j:support event="onchange" reRender="peek" />
</h:selectOneMenu>
<br>
<h:outputText id="peek" value ="#{indexBean.model.selected_id}"/>
<br>
<a4j:commandButton value="Go to Form" action="form" styleClass="indexItems">
<f:param name="selected" value="#{indexBean.model.selected_id}" />
</a4j:commandButton>
The commandButton sends the user to a next page when I want to get the selected position from the selectOneMenu. The problem is that nothing is sent. When I select some value from the Menu the 'peek' outputText is rerendered properly and I can see the correct selection. However it is not sent to the next page and Bean. Surprisingly when I change the value of the parameter to a fixed String it works!!! So i.e. this:
<f:param name="selected" value="someValue1" />
Is read correctly in the next Bean!!! All of the beans are requested scoped with RichFaces #KeepAlive annotation (I tried without the annotation and it's the same). Ihave treied:
-changing a4j:commandButton to h:commandLink or h:outputLink
-changing indexBean.model.selected_id to indexBean.selected_id
-finally changing the input source from h:selectOneMenu to h:inputText
Nothing helps - it is still the same - a hardcoded String is passed correctly to the next page, but when I try to use the expresion the parameter is always empty.
Here I post a snippet of the faces-config.xml:
<managed-bean>
<managed-bean-name>indexBean</managed-bean-name>
<managed-bean-class>id.webapp.beans.IndexBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>model</managed-bean-name>
<managed-bean-class>id.webapp.beans.Model</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>formBean</managed-bean-name>
<managed-bean-class>id.webapp.beans.FormBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>selectedValue</property-name>
<value>#{param.selected}</value>
</managed-property>
</managed-bean>
Does any one have an idea why this doesn't work? I have used the managed-properties parameters like that in the past and it worked (meaning using some dynamically changed values not a static String).
The <f:param> is evaluated during the initial request (when the form is to be displayed), not during the form submit request. So it still remains the initial value, not the changed value.
Give the <a4j:commandButton> an id and refer it in the reRender as well so that its <f:param> get re-evaluated before you press the button.
<h:selectOneMenu id="selectMenu" value="#{indexBean.model.selected_id}" styleClass="indexItems">
<f:selectItems value="#{indexBean.myModelValues}" />
<a4j:support event="onchange" reRender="peek,button" />
</h:selectOneMenu>
<br>
<h:outputText id="peek" value ="#{indexBean.model.selected_id}"/>
<br>
<a4j:commandButton id="button" value="Go to Form" action="form" styleClass="indexItems">
<f:param name="selected" value="#{indexBean.model.selected_id}" />
</a4j:commandButton>

Passing a parameter with h:commandButton

I have a a4j:commandButton which is supposed to redirect me to an appropriate "Edit" page based on an Id, which I wanted to pass as a parameter, something like this:
<h:commandButton action="/details.jsf?faces-redirect=true" value="details">
<f:attribute name="id" value="#{bean.id}" />
</h:commandButton>
The problem is, it doesn't work. I also tried replacing f:attribute with "f:param name="id" value="#{bean.id}" ", but it also failed. The only thing I got to work is an outputLink:
<h:outputLink value="/details.jsf">
link
<f:param name="id" value="#{bean.id}" />
</h:outputLink>
But I'm not really happy with a link, so is there a way to make the commandButton work?
Oh and I also have a bean which is supposed to get that "id" after the redirect:
#PostConstruct
public void init(){
id= resolve("id");
}
Have a look at this article about communication in JSF, by BalusC
f:param only works with h:commandLink and h:outputLink.
You can use an input hidden:
<h:form>
<h:commandButton action="/details.jsf?faces-redirect=true" value="details"/>
<input type="hidden" name="id" value="#{bean.id}" />
</h:form>
And then in your faces-config, I guess is request scoped. If you use the annotations of JSF2, just translate this to the proper annotations.
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>mypackage.Bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>id</property-name>
<value>#{param.id}</value>
</managed-property>
</managed-bean>
You need obviously to have getters and setters for that field in the backing bean.
or try to "paint" the link as a button through CSS.
Unless my JSF is extremely rusty, the action attribute on a command button or command link is used to specify the outcome string defined in your faces-config-nav file, or it should point to a method on the bean which will return an outcome (or redirect/whatever).
In your case, if you want to redirect to another page... you should define that in your config file, as a navigation link (with redirect if necessary). Then in your action button you should have something like
<h:commandButton action="showDetails" value="details">
...
<navigation-case>
<from-outcome>showDetails</from-outcome>
<to-view-id>/details.jsf?faces-redirect=true</to-view-id>
</navigation-case>
As an aside, the <f:atribute> tag will work, but it will only set the attribute onto the component. So if you got a hold of the command button in your bean, you could be able to get the attribute value by name. To pass a request param, use the hidden field technique like pakore mentioned

Resources