Hey there! If you have come here then you are looking for a solution on how to get long running struts action working faster and improve the user experience and have a proper flow.
With struts and actions we have below problems:
- Long running struts actions don’t have any way of handling. This should however not be a practice from start but some application functionalities require the same.
- User can submit again
- User can refresh page which trigers a resubmit again
- User needs to wait for the entire action to complete
There are ways to address all of above and improve user experience.
Apply PRG Pattern
This is first step towards enabling single submit approach. PRG stands for POST, REDIRECT and GET. The approach is when the form gets submitted, the struts action should redirect the output instead of forwarding and get enable a get to poll the status.
The PRG problem and solution is best explain by these wiki diagrams.
This will disable resubmit if user refresh the page and anyway there is no submit button for user.
Now, this is straight foward. Below are configurations needed.
Enable redirection in struts-config.xml
Create a thread framework to handle the action
I will create a separate post for the same. Please watch this space for part 2.
Create Get API to Poll Request
This is pretty simple. Just a simple a simple jersey rest API to poll the status and show on the JSP page. This should be the forward of the redirection.
Disable Back and Submit
This is pretty tricky. There are many ways to disable BACK button of browser but unless you are create a new application this is not the best way. There are many things we can do to prevent/warn the user about the impact.
- Use body “onbeforeunload” to warn the user that BACK/REFRESH is going to have an impact on data.
- When the form is submitted, store a variable/token on the client side preferably in local storage to mark that the user has submitted the form. This can help to avoid resubmit by checking the value.
- Always include some timestamp in page urls to allow the client hit the server where validation can be done to ensure cache is not served. This can help to again ensure that proper data is submitted to struts action.
Part 2 coming soon where I describe the framework that I used to handle offline processing of struts actions.