Explicit url redirect in JSF 2.0 - url

I have the following two pages below.
hello.xhtml, rendered by .../hello.jsf url.
<h:form>
<h:commandButton id="submit" value="Submit" action="response"/>
</h:form>
response.xhtml, rendered by .../response.jsf url.
<h:form>
<h:commandButton id="back" value="Back" action="hello"/>
</h:form>
When Submit button is clicked, the hello page is redirected to the response page but the url remains the same, i.e., the url is still .../hello.jsf.
I'd like the url to be .../response.jsf after the Submit button is clicked. Any help, please?
Many thanks!

If you are actually not using old fashioned JSF navigation cases as implied by kolossus, but are instead using new JSF2 implicit navigation feature, then just add faces-redirect=true query string parameter to the outcome.
<h:form>
<h:commandButton id="submit" value="Submit" action="response?faces-redirect=true" />
</h:form>
However, if you don't need to invoke any bean action at all, and intend to use plain page-to-page navigation, then it makes no sense to perform a POST request for this. Just use the <h:button> then.
<h:button id="submit" value="Submit" outcome="response" />
See also:
Difference between h:button and h:commandButton
When should I use h:outputLink instead of h:commandLink?

Add the redirect attribute to your faces_config.xml file like so:
<navigation-rule>
<from-view-id>/hello.xhtml</from-view-id>
<navigation-case>
<from-outcome>response</from-outcome>
<to-view-id>/response.xhtml</to-view-id>
<!--the redirect element -->
<redirect/>
</navigation-case>
</navigation-rule>
EDIT: I got the impression that you're using a faces-config.xml from your navigation outcome styling. I have confirmed personally that what you're doing is also permissible for the new JSF 2 style navigation. See BalusC's answer for using the redirect url parameter in a JSF2-style navigation handling

Related

JSF Validation error not printed in open dialog

I have to validate that a field in a form is not empty.
The problem is that the form is in a JQueryUI dialog (since I don't know how to create "windows" in JSF), so when I click the commandButton the page is refreshed and the JQuery dialog is lost. After refreshing, I can see the error message was printed in the page code by looking at the source code. If I use ajax in the commandButton then the page is not refreshed but I don't get the message printed, I got it as a sort of "Javascript alert" in the browser.
How can I get the error message without refreshing the page?
<h:inputText value="#{myController.name}" id="myname" required="true" requiredMessage="Name required">
<f:ajax />
</h:inputText>
<h:message errorClass="..." for="myname" />
Note: This looked similar to my problem but I don't think the solution is what I'm looking for.
Note: I am not using additional libraries like Primefaces, etc
With the hint of the comment, I was able to solve it this way:
In the commandButton, use ajax with both render and execute (render only doesn't work):
<h:commandButton id="...." action="#{myController.doSomething()}" type="submit" value="Do something">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
Create a "pageUpdate()" method in myController that is called at the end of myController.doSomething(). This way, after the actions, the page is refreshed and the dialog is gone. There are of course other ways to close the dialog, but it is convenient for me to update the page when there is no error in the dialog action, so my objects in memory are updated too.
I think JQuery dialog messes with my styles after the ajax call returns with error, so check that out if you do it.

PrettyFaces url routing from form submit

I'm using Pretty Faces to do URL rewriting, in order to be able to reuse some xhtml files. I'd like to have URLs like '/honda/index.xhtml' and '/toyota/index.xhtml' both go to the same /make/index.xhtml file, with the make coming in as a parameter. This routing seems to work OK with a config like this:
<url-mapping id="carMake">
<pattern value="/#{make}/index.xhtml"></pattern>
<view-id value="/make/index.xhtml"/>
</url-mapping>
I also have this mapping for a search results type of page:
<url-mapping id="search">
<pattern value="/#{make}/search/index.xhtml" />
<view-id value="/search/index.xhtml" />
</url-mapping>
Both of these work as expected when I manually put the URL in the browser.
I run into a problem when I try to put a form on the first page, that I want to redirect to the second page. I have this jsf xhtml code for a form:
<h:form>
<h:messages />
<h:inputText id="searchTerm"/>
<h:commandButton value="search" action="/honda/search/index.xhtml?faces-redirect=true"/>
</h:form>
(hard coding /honda here to simplify the example)
When I try to submit this search, it bounces back to the same /honda/index.xhtml, with no messages being displayed on the page.
The logs show this:
09-23 11:39:55 DEBUG PrettyNavigationHandler:57 - Navigation requested: fromAction [/honda/search/index.xhtml?faces-redirect=true], outcome [/honda/search/index.xhtml?faces-redirect=true]
09-23 11:39:55 DEBUG PrettyNavigationHandler:60 - Not a PrettyFaces navigation string - passing control to default nav-handler
I've tried without the faces-redirect param, but got the same result.
Why does /honda/search/index.xhtml work when I put it into the browser directly, but not as the result of an action?
If you want to use separate the URL from the View ID, and you don't want to reference the view-id in the app, then you need to use pretty navigation strings:
<h:commandButton value="search"
action="pretty:honda"><f:param name="make" value="honda" /></h:commandLink>
But this really is just making it more complicated than it needs to be. I would recommend doing what #chkal suggested, except his example is a little wrong. It should have been:
<h:commandButton value="search"
action="/search/index.xhtml?faces-redirect=true&make=honda"/>
This should be covered in the docs :) http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/components.html#components.prettylink Check that section (and the ones below it) and see if that helps!
You cannot use a pretty URL as a value for the action attribute this way. You have to use a standard JSF outcome with make being a query parameter.
Try this:
<h:commandButton value="search"
action="/honda/search/index.xhtml?faces-redirect=true&make=honda"/>

selectBooleanCheckbox doesn't initiate viewstate id

I have a backing bean which is ViewScoped. I have some ajax enabled SelectBooleanCheckboxes in a form.
Example:
<p:selectBooleanCheckbox value="#{formBean.value2}">
<p:ajax update=":theform"/>
</p:selectBooleanCheckbox>
Normally, some hidden input fields should be in the html to pass through the viewstate id. The problem is that those don't get initiated on the first page request. When I click one of the checkboxes, the form (and thus the checkboxes) get refreshed. From this moment the view state id is being sent with those checkboxes so the viewstate can be preserved.
The biggest problem in this is that on the first ajax call a set of new ViewScoped beans is initiated and so I lose data about the first action. From then on everything seems to work properly.
Can anyone give me any directions on what might be the problem?
The exact code:
<p:panel header="Schedule" toggleable="true">
<form>
<p:outputPanel id="schedule">
<ui:repeat var="scheduleDay" value="#{jobBean.jobScheduleDays}">
<div>
<p:selectBooleanCheckbox value="#{scheduleDay.selected}" style="margin:4px">
<p:ajax update=":schedule" />
</p:selectBooleanCheckbox>
<h:outputText value="#{scheduleDay.readableDay}" style="text-transform:capitalize"/>
</div>
</ui:repeat>
</p:outputPanel>
</form>
</p:panel>
Your problem is caused by using plain HTML <form> instead of JSF <h:form>. The <h:form> will not only generate the right HTML <form> element with the proper method and action URL set, but it also takes care of generating two additional hidden fields identifying the form being submitted and the JSF view state identifier.
E.g.
<h:form id="foo">
generates
<form id="foo" name="foo" action="ViewHandler#getActionURL()" method="post">
<input type="hidden" name="foo" value="foo" />
<input type="hidden" name="javax.faces.ViewState" value="..." />
(the form action URL is filled with result of ViewHandler#getActionURL())
That the plain HTML <form> works with JSF ajax requests is because they don't exactly use the form element to send the HTTP POST request, but instead uses JavaScript's XMLHttpRequest object to send the HTTP POST request whose parameters are been populated by traversing the input elements in the parent form using JavaScript's form.elements. The JSF ajax response will automatically update the JSF view state hidden field, that's why JSF will be able to process the form submit from the 2nd request and on.
However, using plain HTML <form> would have failed hard when using non-ajax requests.

JSF2 Dynamic form is not being submitted

I have a form that is defined in a separate jsf page. This is is included to the main page when i click a link. Now the form is being displayed correctly. But the problem is that the submit button is not calling the action function defined.
The code to include the page( As suggested in this question: JSF2 Dynamically loading pages by ajax
<h:panelGroup id="editdivparent" layout="block">
<h:panelGroup id="editdiv" rendered="#{formsBean.edituserdiv}" layout="block">
<h:form id="userform" class="form-horizontal">
<ui:include src="edituserdetails.xhtml">
</ui:include>
</h:form>
</h:panelGroup>
</h:panelGroup>
The included page contains just the form elements with submit button:
<h:commandButton action="#{userBean.register() }" value="Update">
</h:commandButton>
I am getting no errors. On submitting the form the current page is redisplayed. I have put some print statements in the action function. Also there is a query error is put. None of them are being generated.
Am i doing something wrong here?
The commandButton action property already expects a method expression, so just take the parenthesis out, like this:
<h:commandButton action="#{userBean.register}" value="Update" />
Also, make sure you don't have nested forms when making templates with includes.
I hope it helps.

"This link is deactivated, because it is not embedded in a JSF form."

When I use the following command link:
<h:commandLink action="student" value="students" />
And the following navigation rule in faces-config.xml:
<navigation-rule>
<from-view-id>/home.xhtml</from-view-id>
<navigation-case>
<from-outcome>student</from-outcome>
<to-view-id>/student.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Then I get the following development stage faces message:
This link is deactivated, because it is not embedded in a JSF form.
How is this caused and how can I solve it?
The <h:commandLink> fires a POST request. You need to embed it in a <h:form>.
<h:form>
<h:commandLink action="student" value="students" />
</h:form>
Since you're already on JSF 2.0, you can also just use <h:link> instead which fires a GET request which doesn't require a form and is thus way much better for bookmarkability and SEO. Also you can get rid of the whole <navigation-rule> since JSF 2.0 utilizes implicit navigation.
<h:link value="students" outcome="student" />
It will implicitly go to student.xhtml.
Ensure that you're reading JSF 2.0 tutorials, not the ones targeted on JSF 1.x. In JSF 2.0 a lot of new tags and features have been added.
See also:
When should I use h:outputLink instead of h:commandLink?
We don't need stinkin' faces-config
You need to have <h:form> wrapping the link.

Resources