Rendering Page in SessionScoped - jsf-2

I am facing a Problem using #SessionScoped in JSF. When traversing from Home.jsf to Dashboard.jsf with some Query String parameters, Dashboard.jsf page is working fine as per the input in QueryString parameter.
But, when i am going back again(BROWSER BACK BUTTON) to Home.jsf and click on some other link for Dashboard.jsf, the same is not updating the Page as per the Query String. It is displaying me the Same page i requested previously.
When i debug the code, i observed, that on first request it is going to code behind PageBean(Dashboard.jsf), but, on another request, it's not.
I don't want to use #RequestScoped and #ViewScoped because i've to maintain the values in the Session.
Can anybody suggest how can i render the Dashboard.jsf on every request?
Please Suggest.

Related

JSF Page Navigation+Data flow

I am new to JSF and started learning and have worked on struts. Just wanted to understand how navigation works in JSF2.0. I understand in JSF2.0 navigation rules can be done in faces-config.xml. But I would like to understand how it works in real world.
Suppose I have a screen to enter search criteria and the next screen should dispaly the search results.Where do I process the result set <h:commandButton value="Show Results"
action="#{simpleController.doNavigation}"/>and my doNavigation() does the job of querying the database and fetch the results, how do I carry it next screen. Do I need to hold the value in SESSION
Just assign the data as a property of the current bean instance.
public String doNavigation() {
results = searchService.find(query);
return "nextpage";
}
By default, navigation doesn't fire a new request, it just sets the target page to the current response. In the target page, you can just use #{simpleController.results}.
Unrelated to the concrete problem, this is a poor practice (bad for SEO and UX). Rather show the results in the very same page and don't perform navigation on postbacks.
See also:
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Problems with navigation cases from an embedded JSF 2.0 application

Browser back goes two JSF pages back

I have a JSF page with a form. The submit button is an <h:commantButton> with an action that calls a backing bean function, which in turn does some calculations and then returns a string which is the name of the next page to view.
This works as expected. On this page is an <h:link> with an outcome of another JSF page. When on this third page if the user clicks the browser back button they are taken to the first page with the form and not the second page. Why is this? I should mention that the backing bean is session scoped.
Thanks,
Doug
This is normal, if you verify your URL, you will see that it doesn't change when you click on the h:commandButton. If you want it to change correctly in the first place, you can modify your action like this : youraction?faces-redirect=true.
More info :
JSF : Page Forward vs Page Redirect

Difference in navigation by action="xyz" and action="#{bean.returnXyz}"

How is navigation from a Facelet page
<p:commandLink action="xyz.xhtml">
or a backing bean
<p:commandLink action="#{bean.redirect}">
public class Bean{
public String redirect(){
.....
return "xyz.xhtml";
}
}
different from each other?
How is navigation from a xhtml page or a backing bean different from each other.
There's no difference. The both examples invoke a POST request and instructs JSF to render the view associated with the given outcome. The backing bean method has the only advantage that it allows you to perform some business logic beforehand or even control the outcome value programmatically.
However, if you don't have any business logic at all and solely want to have an idempotent link to another page, then using a command link is actually a bad practice. Using POST for page-to-page navigation is not user nor SEO friendly. The target page is not bookmarkable (the URL remains the one of the page where the POST form was been submitted to) nor searchbot-crawlable (it is using JavaScript to submit a hidden form).
You should instead use a normal link.
<h:link outcome="xyz.xhtml">
This generates a SEO-friendly <a> element with the full URL in its href and ends up in an user-friendly bookmarkable URL.
See also:
When should I use h:outputLink instead of h:commandLink?
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Check out the documentation of p:commandLink here, which says the following for action attribute:
A method expression or a string outcome to process when command is
executed.
Now, as action="xyz.xhtml" returns String xyz.xhtml you're redirected accordingly and for action="#{bean.redirect}" which again returns xyz.xhtml you are again redirected according to the returned String.

jsf clearing the form

I need help. I'm new to JSF and im using JSF2 and richfaces in my project.
I want to clear the form for which I'm using <f:ajax render="#form"/> in refresh button. I have an ADD button on that screen which adds one record and I hit refresh then it's going to the default page. But when I once again go to enter a record then those values which I entered earlier remain in the form fields.
Can anyone please help me out with this issue?
Assuming that you mean the browser's refresh button when you say "I hit refresh", then that can happen if you've incorrectly placed the bean holding view scoped data in the session scope. You're then basically reusing the very same bean as the form is previously been submitted to. Putting the bean in the view scope instead of the session scope should fix this problem. The view scope ends when you navigate to a different page or fires a new request (as by hitting browser's refresh button).
See also:
How to choose the right bean scope?
Update if you're due to bad design restricted to using session scope, then you might want to hack this around by a
<f:event type="preRenderView" listener="#{sessionScopedBeanWhichShouldActuallyBeViewScoped.resetModel}" />
with
public void resetModel() {
if (!FacesContext.getCurrentInstance().isPostback()) {
model = null;
}
}
This will clear the model on every GET request. However, regardless of this hack, you'll still run into serious problems when the enduser opens the same view in a different browser tab/window within the same session.
The right solution is to put the bean in the view scope instead of the session scope, as said earlier.

Post/Redirect/Get: Redirect to specific route

I have the following scenario:
I have an edit page, which can be called from different pages. These pages could be the detail view for the current entity, or the list view for the entities (with or without a search in the route).
HOW do I cleanly redirect to the original calling page using the MVC framework? Of course I could simply pass the HttpContext.Request.Url value by holding it in my TempData, but that sort of smells, in my eyes (or, err, nose). It's on a lower level than the rest of the code.
Is there a way to get the routevalues for the previous page in a controller context? If I have that, I could store that temporarily and pass that to the redirect.
Do not use TempData when not redirecting. One AJAX request from your edit page, and the TempData will go away.
Tomas is right that a hidden element or query string parameter is the way to go. But make sure you sanitize the value submitted. You don't want to redirect any old site on the web; you need to ensure that the page to which you redirect is part of your sites.
you can always have a hidden form element telling the controller where to redirect when posting a form. when using a get request, you could use a querystring in a similar way. it might not be the most beautiful solution, but it's quite a lot safer than trusting httpreferrer or other headers that could easily be changed (or ommitted) by the browser.

Resources