Question

I'm building a web application using .JSP pages and the Struts2 framework. I want to create a custom interceptor that will kind of act like the viewstate in ASP.NET.

When an action is invoked, this interceptor should fetch all the data a user entered into any input fields on the .jsp page the user is coming from. The interceptor will then put this data into the session. The next page to be shown (the result of the action, possibly the same page as before in case of postback) will then be able to fetch this data from the session again and fill in the input fields again if necessary.

My problem lies with detecting whether any input fields occur on the current page, and if there are any, to get their text. It would be convenient if my interceptor could somehow read the DOM of the page, and loop over all input elements. unfortunately, I cannot find any method to give an interceptor access to the content of the .JSP page right before the action/postback occured. Can this be done?

I know these values get put on the valuestack automatically when using forms and a submit button, but in this case I'm not always pressing submit, I might also be pressing random other buttons. For example, my page might a form, but also 3 language buttons that let the user switch between languages. pressing one of these buttons will not actually submit the form and thus the data from the input will not be put on the valuestack. Even if it were, every form might have different input fields, so I cannot access the valuestack with the name of the input fields because my viewtsate interceptor would not know beforehand which inputs are present.

One other possibility I'm epxloring now is the following setup:

*Whenever a user enters anything in an input field, use javascript
to put that value into the httpsession.
*Whenever a user goes to page, use javascript to populate all input fields
if there is a corresponding value present in the httpsession.

I'm not sure if this will work, and it's also a pity it wil not use the struts framework (in case javascript is disabled or something)

No correct solution

OTHER TIPS

I ended up using HTML5's localstorage:

  • It is fast, easy and secure
  • supported by all browsers (IE: starting from IE9)
  • It is clientside only, so no need for traffic to the server
  • It is true users can clear their cache, but they likely will not do it while performing actions on the website, so this is not a problem.

  • Minor downside: javascript mus be enabled for it to work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top