Spring MVC Application (Back button) - spring

On my MVC spring application I send a form to a page using GET method to perform a search. The results of the search is a list and, for every entry is possible to navigate to a details page. It works fine.
Now, I want to have a back button on the detail page which should take back to the searched list.
I can call the same controller method to reevaluate but it performs Data Base calls, which I don't want to do.
What is the best solution?

You want it to perform database calls don't you ? What if the data has been changed by a differet user?
If not you can use javascript :
<input action="action" type="button" onclick="history.go(-1);" />

Related

I need to introduce an intermediate page before I load the actual page for every request

I've a web application that uses FreeMarker templates and tiles to do the View job in the MVC world. So upon my request to the application, say /load.do I would like to introduce an intermediate page that will have a loader icon just to indicate the page is being loaded on the subsequent request, which is not known to the user.
So ultimately what I'm achieving is a better user experience and also if used within framework (the iFrames) this comes handy to show the loader icon when there is a new request that is happening.
Can someone point me to the right method I can use here ?
This is possible using spring mvc.
You need to create something like a WaitController. On user submit , this controller will be called. This can trigger your original request in background and take a reference(UUID) for the call and will render a wait page having the reference id of the original request set in model. Wait page can either trigger immediately the main controller using reference id or can poll the reference id on regular intervals or use web socket to know the availability of results. Once results are ready, it should redirect user to the actual result page.

Flowscope and get request

I'm experimenting the new "flow" functionality.
It seems very promising because it lets you have a managed bean which spans across multiple related views.
Unfortunately it works only with post requests.
Is there a way to enter a flow using a get request ? All the few example I found use a starting form outside of the flow. I would like to enter a flow clicking on an item inside a Primefaces menubar and, as far as I know, I can only put a link there...
Suppose then that a user bookmarks a page in the middle of a flow. If a get request for a view in the middle of a flow is sent and the flow is no more active (or flow information are removed from the querystring parameters) the server responds with a bad error page. In such cases is it possible to be redirected to the first node of the flow ?
Navigating through the view nodes of a flow I can see a special parameter in the querystring which most likely is an ID. Is it possible to hide that detail ?
Thanks
Filippo
Yes, just if the flow is named flow1, you can write something like:
<h:link value="Enter Flow" outcome="flow1"/>
That's it.
About the navigation, there is nothing that handles that in a explicit way, but you can override FlowHandler implementation, specifically the method clientWindowTransition(...) and check in that part if the flow is active and do what's necessary. To get out from a flow under a navigation outside of the flow you can override ViewHandler.createView(...) method and add the transition.
The flow state is bound to a client window id, which is what you are seeing as a query param. This detail is necessary because it provides an state that persist across navigation, but that is not as large as session state, which comprise multiple windows or tabs.
I recommend to use Apache MyFaces JSF 2.2 Implementation because the solution there has taken into account cases like multiple nested flows. It works pretty well. Take a look at this JSF 2.2 examples from Michael Kurz JSF Live blog on Github, that could be helpful.

What is the best way to handle a session-persistant search form in a Spring web application?

I am looking for the best way to handle a session-persistant search form in a "shopping cart" like web application using the Spring MVC Framework.
I want to be able to navigate back to this search page, with last filters already set, from any other page in the application. This is not a master detail search results page, only a form with filters on a table of elements displayed underneath.
I can store my search filters in the user session, but what about multi-tabs navigation and browser back button handling ?
I also considered using Spring WebFlow to adress this.
Any suggestions ?
That sounds like a good job for the conversation scope of Spring WebFlow. Objects which are stored in this scope are saved until the current flow is terminated (or by timeout). The usual way to use it in your case would be to create a new flow/conversation when the user starts browsing the webpage and saves the search parameters in the conversation scope. When going back to the search page later, the parameters are retrieved from this scope (if there are available).
The conversation scope solves the multi-tabs problem and avoid to have to send back to the server every time the data (as you would have to do if you only use the request scope).

MVC 3. Load menu and content separately

I have an app with 3 sections:
Main menu;
Context Menu - Related to selected item in main menu;
and Page body - Related to selected item in context menu;
"Main menu" and "Context menu" are based on membership. I don't want to load them everytime my page loads, because that would consume resources database. So, I'm using ajax to load main menu only one time, and when an item is selected, I load the context menu for that item.
My problem is: Every form's post will erase my menu.
Question: Will I have to build my entire application using ajax? I don't wanna do that, because it is too much simpler do a post in the form then send all data to controller with ajax.
Until now, I have 2 options:
Load my menus with ajax and the page body with IFRAME, so the post's will not render again my menus.
Do everything using ajax;
Is there any alternative to load my menus with ajax and be able to use form's post?
Sorry if I wasn't clear enough.
The sentence that gave me a pause is this "I don't want to load them everytime my page loads, because that would consume resources database."
You see, I've build quite a lot of apps, that display menus and sub-menus based on user roles (what you called membership). This has never been an issue from the resources or database perspective.
You can access all the membership information that you need once, when your used is being logged in. In the simplest case user's identity will be stored in the context along with the roles they have (HttpContext.User), so you do not to need a database lookup at all to get this information on every request. Note that with this scenario no ajax is required either.
If for whatever reason you can't store your membership information in the context like this, you still can store in in session (if in-memory) or in encrypted Cookies.
Now, I understand, that I don't all the details of your scenario, and that may be in your scenario what you are trying to do is warranted, however I suggest you think it through again, as under normal circumstances what you indicate is a problem (database resource) should not be a problem at all.
The bottom line is: if you alter your application that it stores the membership information when user logs on you won't have your problem to start with.
You don’t have to build all of your application using Ajax. But in this scenario Ajax may be the best way forward.
Following is my suggestion
Create your data entry for inside a dev
Have each input controller marked with a class (say ‘dataEntry’)
Create a javascript function to iterate the dev and build a list of all elements that has class dataEntry
Build a json object using the list. Use the name of each element as property name and value as the property value
Use jquery ajax to post this to the controller action
[optional] you can use .done and .fail methods to take action on success or failures of the call
I know this may look intimidating, but if you have many data entry forms, you can re-use this code.

Prevent page reloading in Struts 2

I am currently working on a small web app and this is the first time i am using Struts2. Here is what i am trying to achieve.
A Struts2 JSP page on form post, calls a struts action. Once this action completes it task, I need to return to the calling JSP with out any page reloading (avoid postback).
how can i achieve.can any one please help me?.
It is called AJAX (no kidding).
In Struts2 you can use Struts2-jQuery Plugin to achieve that pretty easily.
As AndreaLigios said, you can achieve this using struts-j query plug-in.
Create a remote page to display your result.
use
<sj:sbmit targets="[div_id]" />
on your main page to submit the form
Add a div to your main page to display the remote page.
<div id="[div_id]" />
In your struts.xml, just return the remote page as a result of your action.

Resources