Richfaces 4 fileUpload redirect following uploadcomplete - jsf

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.

Related

h:commandButton's action not being invoked [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
i'm using JSF 2.0 and I'm having a commandButton inside div section in home.xhml. it's not working. I don't see the destinationPage, remaining only in home.xhtml.
home.xhmtl:
<div id="left_side">
<ui:include src="login.xhtml" />
<h:form>
<h:commandButton value="register" action="register"/>
</h:form>
</div>
register.xhtml is inside a h:body and a h:form
and the faces-config.xml configuration:
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
If home.xhtml and register.xhtml are at the root of your application, then you need to define the navigation rule as follows:
<navigation-rule>
<from-view-id>/home.xhtml</from-view-id>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Otherwise you add the path relative to the root.. which would be indicated by a backlash
Update
Actually in your case it seems that you do not really need the POST request which is generated by thecommandButton. A simple button which would generate a GET request seems to be sufficient:
<h:button outcome="register" value="register" />

Navigation rule not firing

I have a navigation rule that looks like this:
<navigation-rule>
<display-name>forgotPwd</display-name>
<from-view-id>/login/forgotpwd.jsf</from-view-id>
<navigation-case>
<from-outcome>pass</from-outcome>
<to-view-id>/login/pwdresetcomplete.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/login/forgotpwd.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
So I do this to trigger the navigation rule:
Navigate to /login/forgotpwd.jsf?uuid=fed8b3f7-ed33-4941-8306-3d2afbb8d1d0
This page has the following form:
<h:form id="resetForm">
<!-- Omitting the login fields -->
<h:commandButton type="submit" id="resetPassword" value="Save" styleClass="btn small" action="#{registration.resetPassword}" >
<f:param name="uuid" value="#{facesContext.externalContext.requestParameterMap.uuid}" />
</h:commandButton>
</h:form>
(Takes the uuid passed as a query string parameter, and will call the registration.resetPassword function.)
Here is the registration.resetPassword java code:
(Details omitted for brevity)
public String resetPassword() {
// If good
return "pass";
// If bad
return "fail"
}
Problem: When it returns "pass", the navigation rule is not firing. It goes back to /login/forgotpwd.jsf instead of /login/pwdresetcomplete.jsf
Is this because it has the UUID parameter appended to it? Why is my navigation not firing?
Is there some log4j logging that I can trigger to see why this isn't working?
<from-view-id>/login/forgotpwd.jsf</from-view-id>
The from-view-id must be the view ID, not the virtual URL. So, it should not contain .jsf extension.
<from-view-id>/login/forgotpwd.xhtml</from-view-id>
By the way, request parameters are in the view available by #{param}. This saves some characters as compared to #{facesContext.externalContext.requestParameterMap}.
<f:param name="uuid" value="#{param.uuid}" />
See also implicit objects in EL. Another by the way, it's not exactly clear which JSF version you're using, but since JSF 2.0 (which I guess you're using as you're on Facelets already) you don't need the navigation case XML hell anymore. You can just return the (relative and/or extension-less) to-view-id directly from the action method.
public String resetPassword() {
// If good
return "pwdresetcomplete";
// If bad
return "forgotpwd"
}
This way you can get rid of the whole <navigation-rule>. See also implicit navigation.
I added these two rules... now it works. Not sure which one did it but using the from-action made it a lot more happy.
<navigation-rule>
<display-name>forgotPwd</display-name>
<from-view-id>/login/forgotpwd.xhtml</from-view-id>
<navigation-case>
<from-action>#{registration.resetPassword}</from-action>
<from-outcome>pass</from-outcome>
<to-view-id>/login/pwdresetcomplete.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{registration.resetPassword}</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>/login/forgotpwd.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>forgotPwd</display-name>
<from-view-id>/login/forgotpwd.jsf</from-view-id>
<navigation-case>
<from-action>#{registration.resetPassword}</from-action>
<from-outcome>pass</from-outcome>
<to-view-id>/login/pwdresetcomplete.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{registration.resetPassword}</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>/login/forgotpwd.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

How to access inputtext binding in 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>

Navigation to same page in JSF1.2

I am having a h:commandButton button named cancel on a JSF1.2 page, when a user press this button he should be redirected to the same page. How it can be achieved in JSF1.2?
First remember to set immediate="true" attribute, by default, the tag will invoke all the validators etc.., before invoking the application.
Your button:
<h:commandButton value="cancel" action="cancel" title="Cancel this page" />
Your faces-context.xml:
<navigation-rule>
<from-view-id>index.xhtml</from-view-id>
<navigation-case>
<from-outcome>cancel</from-outcome>
<to-view-id>index.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>

how to call a page on a jsf button click

I'm using JSF, trying a to call a page on a button click inside a data table using the following code
<h:column>
<h:outputText value=""/>
<h:commandButton id = "btn" value="History" action="#{Bean.showHistory}">
</h:commandButton>
</h:column>
My bean class icludes a method "showHistory" which returns success and I've included the navigation rule in faces-config.xml too but still the page couldn't be called. My navigation rule looks
<managed-bean>
<managed-bean-name>Bean</managed-bean-name>
<managed-bean-class>
com.bean.HistoryBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>List</display-name>
<from-view-id>/BookList.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/History.jsp</to-view-id>
</navigation-case>
</navigation-rule>
Bean Class consists of showHistory() as
public String showHistory() {
return "success";
}
Can I get a help to resolve this????
Ensure you put the command button inside a h:form element: it should work!
From your source code everything looks good (of course including Pier's suggestion to use the h:form tag).
Try to debug and check if your action method is called at all.
If not, then maybe your from-view-id element does not match. As a test you could try it with an empty element (default navigation for all pages):
<from-view-id></from-view-id>
If this works, then the problem is with your from-view-id element. Maybe it's in a different folder or an upper/lower case typo.

Resources