java - how to manage session attributes due to post requests, and history -
i have following problem:
i want send information between modules (different controllers) using post due security reasons. logic has been this:
user searches > clicks on specific item > sends form post request controller > controller shows view of specific item > user clicks on sub-item page > sends form post request sub-item's controller
however, because of how post works out, it's giving me "webpage has expired" messages when going subitem page item page.
my solution problem save these parameter(s) in java's httpsession
, post. not particularly sure how go @ it.
for example here snippets of code (for record i'm using thymeleaf view resolver):
search.html snippet
<tr th:each="customer:${results.pagelist}"> <td> <form method="post" id="gotouser" name="gotouser" action="/customer/"> <input type="hidden" name="acctcustnbr" th:value="${customer.acctcustnbr}"/> <a href="javascript:;" onclick="javascript:document.getelementbyid('gotouser').submit();" th:text="${customer.acctcustnbr}">000010</a> </form> </td> <!-and on-->
customer (or item, in example) controller receives request:
@requestmapping(value = "/customer/", method = requestmethod.post) public string getcustomer(@requestparam(value = "acctcustnbr", required = false) string acctcustnbr, model model, httpsession session) { boolean error = false; string errormsg; logger.info("acctcustnbr obtained >" + acctcustnbr + "<"); if(acctcustnbr==null){ acctcustnbr = (string) session.getattribute("acctcustnbr"); } else session.setattribute("acctcustnbr", acctcustnbr); /*service methods , model additions*/
now how past initial stage? seems sub-item's controller can obtain session attribute fine, hitting on browser still opens dreaded expiration alert.
my logic if user hitting button /customer/
page send acctcustnbr==null
. i'm wrong because doesn't work.
so question is, doing wrong here?
solution @skirsch
i renamed controller method this:
@requestmapping(value = "/customer/", method = requestmethod.get) public string getcustomer(model model, httpsession session) { boolean error = false; string errormsg; /** service invocations , stuff**/
and added one:
@requestmapping(value = "/customer1/", method = requestmethod.post) public string storeacctcustnbrinsession(@requestparam(value = "acctcustnbr", required = false) string acctcustnbr, model model, httpsession session) { session.setattribute("acctcustnbr", acctcustnbr); return "redirect:/customer/"; }
you need redirect browser place after storing data in session. other place be
@requestmapping(value = "/customer/", method = requestmethod.get) public string getcustomerfromsessionvalue(model model, httpsession session) { ... }
as redirect using method, won't experience "dreaded expiration alert".
see redirect after post
Comments
Post a Comment