I was trying to understand how to perform PRG on a JSF app using this link
https://blogs.oracle.com/enterprisetechtips/entry/post_redirect_get_and_jsf
but somehow I got confused on three items.
From the blog, I see from firebug that it is issuing a 302 and GET redirect when rendering pages instead of the POST method which is a PRG.
The only changes in the blog is the scope of the bean from being a session scoped to a request scope. SO does this link suggest that
all my managed bean be at Request Scope level? I check my sample app that I am doing and I am heavily using ViewScoped and SessionScope beans.
Whats the usefulness of ViewParameters? Arent that a security problem, supposed I add more query string parameters at the url?
I am actually finding a use case on where can I used such feature.
If the transaction that I am doing will not do any transactional nature such as ADD/EDIT/DELETE, is it still a good practice to just merely use the JSF navigation which is a POST request? I just wanted to navigate to a new page.
Thanks
So does this link suggest that all my managed bean be at Request Scope level? I check my sample app that I am doing and I am heavily using ViewScoped and SessionScope beans.
The bean's scope doesn't matter for the PRG itself. It only matters for the lifespan of the data the bean holds. See also How to choose the right bean scope?
Whats the usefulness of ViewParameters? Arent that a security problem, supposed I add more query string parameters at the url? I am actually finding a use case on where can I used such feature.
The includeViewParams will only include the parameters which you've manually specified in <f:metadata><f:viewParam>. It will not include all original query parameters. This is not necessary for the PRG itself. Whether to use it or not depends on the concrete functional requirements. The article just shows that it's possible to copy all view parameters into the redirect URL, which may be useful/mandatory for some functional requirements. See also What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
I just wanted to navigate to a new page.
Use <h:link> instead of <h:commandLink>.
<h:link value="Go to next.xhxml" outcome="next" />
See also When should I use h:outputLink instead of h:commandLink?
Related
Let's say I have a form with ten input fields, and each of them has AJAX validation. Can I still use a Request scoped backing bean, or should I use View scoped to keep the page performant? Is it too expensive to recreate the backing bean on each validation request, if there are not too many DB queries? Are there any guidelines when to use View scoped bean in a situations like this?
You should use View scoped beans.
Unless you have a reason to do otherwise, using Request scope will be confusing: page state is not preserved between trips to the server. Using View scope will make your page behave the same way you think about the page: requests go to the server, responses come back, the page saves its state as long as the user does not navigate away from the page.
With 10 input fields, your performance overhead should be negligible. Depending on your application, performance might even improve (!)
As suggested in the comments, if you have serious performance concerns, run a profiler. Convince yourself that what you are doing is correct.
Here are some guidelines on which scope to choose:
How to choose the right bean scope?
Currently I have only one controller AppController (SessionScope - ManagedBean) that handles all of the requests and logic of my system, but somehow this doesn't seem right. I want to make this project as modular as possible so it can be very easy to maintain.
I have read a little about Dependency Injection, but I just can't make it work, that is, I don't know what's the issue with the "scope" of the beans. For example, I have my AppController and my Users bean, but I can't make use of the AppController from the Users bean (although I have tried with dependency injection). I think that the logic of the users (edit names, set relationships, etc.) must be handled by the Users bean, but right now those tasks are handled by the AppController, which doesn't seem right.
The issue is, Is there any good tutorial where I can learn how to make a proper use of the JSF 2.0 framework? My objective is to make the AppController as light as possible. I have found several tutorials, but they seem to be more focused on older versions of the JSF or I just don't happen to understand them (maybe they are too technical, I don't know).
Any help with this would be very appreciated.
As to the concrete problem, you shouldn't have a single managed bean which acts as a front controller to handle all requests. JSF's own FacesServlet is supposed to do that. The common consensus is to have a single request/view scoped managed bean per <h:form> you have. And one or two session scoped bean(s) to represent the logged-in user and its settings like locale, if any. You could have an application scoped bean for applicationwide data such as dropdown constants.
As to the concrete question (which is rather subjective and per definition offtopic here, but ala), there is not really a simple tutorial for this. It's a matter of understanding what you're doing and what the code is doing and ultimately using the right tool for the job. In order to learn that, you've to read a self-respected JSF2 book from top to bottom and play a lot around with simple projects. JSF can't be learnt and fully understood in 1 day. It took me years. You can find useful links/references in our JSF wiki page.
I have a CRUD application.. Where in the data is fetched, displayed n edited n stored back to db. Also there is a sequential access between pages & the information entered on the first page is needed on the second page & so forth. What would be the best scope & why ?
When to use which scope? What is the life-cycle of each scope.
Also, I couldn't find any good tutorial on scopes. If you are aware of the same, please guide.
Thanks,
Shikha
There is a detailed explanation of JSF scopes in BalusC's excellent tutorial Communication in JSF 2.0.
As rule of thumb I would recommend to always choose the most narrow scope in order to reduce the state that is saved on the server side. Your application would have a bad scalability if you kept everything in session scope. The view scope is especially useful if you are working with ajax. For non-ajax views the request scope is often sufficient.
I have a html mockup with lots of css images refs etc. and need to convert it to a jsf 2.0 app.
I know of the facelets attributes jsfc, but as i see it this does not work for jsf tags that need an attribute itself. Say you have a simple login form that needs to be converted. within there is a submit button. does not work, my action will not be called.
jsfc="h:button" action="#{auth.authenticate}"
I am having a hard time to believe that the interaction between developers and designers still is an unsolved problem, after all those iterations in Java EE. Designers do their stuff in html, css, maybe js but no jsf tags or el code. So there must be an efficient way how we can annotate html where the only difference between rendered from the filesysem and rendered from a jsf container is that the later has dynamic functionality.
anyone?
As to the concrete problem as stated in your question, you're confusing <h:button> with <h:commandButton>. The <h:button> sends a GET request and supports a navigation case outcome only and the <h:commandButton> sends a POST request and can invoke a bean action method.
So fix it accordingly as
jsfc="h:commandButton" action="#{auth.authenticate}"
or
jsfc="h:button" outcome="nextPage"
I'll ignore the remnant of your question as that's subjective. Try http://programmers.stackexchange.com instead (only try to make it a bit more constructive, or it'll get closed as non-constructive).
See also:
JSF View Declaration Language Documentation - the ultimate JSF tag documentation
In the application I'm developing in ASP MVC 3 I have the following scenario. I have some properties of the model that I wanna use after the page makes a request, however when the post request is done they arrive as null since they are not bound to any control in the view form.
For what I've read this is the expected behaviour of ASP MVC and people recommend to use a #Html.HiddenFor() to be able to receive them, but I don't want this information to be available to user in case he selects "View source" from the browser.
In JSF I remember that you could use the #ViewScope annotation for this scenario, but is there something similar in ASP MVC? For what I've read saving them in the ViewData property will not work either and I do not want to use the Session because this properties will only be relevant in this particular view.
If I understand your question correctly, you may be looking for something like TempData.
You can read about it here:
http://blogs.teamb.com/craigstuntz/2009/01/23/37947/
A more recent post can be found here:
http://codeoverload.wordpress.com/2011/05/29/controller-tempdata-in-asp-net-mvc-3/