0

I'm currently trying to save search criteria attributes to the session between two pages: search page and edit page. The goal is to save the three variables: sYear, submission, collectionPeriod. I'm adding these to the session here below in the Search screen controller:

request.getSession().setAttribute("sYearSave", sYear); request.getSession().setAttribute("submissionSave", submission); request.getSession().setAttribute("collectionPeriodSave", collectionPeriod);

In the edit screen controller, I set a BooleanisFromEditScreen to true. This is so I know that I'm coming from the edit screen. I do print out the variables and I do get the values correctly here in the edit controller screen.

request.getSession().setAttribute("isFromEditScreen", new Boolean(true));
    sYearSave = (String)request.getSession().getAttribute("sYearSave");
    collectionPeriodSave = (String)request.getSession().getAttribute("collectionPeriodSave");
    submissionSave = (String)request.getSession().getAttribute("submissionSave");

But the problem is when I use a back button to go back to the Search screen, the search criteria sYearSave, collectionPeriodSave, and submissionSave values return NULL. For some reason, the isFromEditScreen boolean works just fine and returns true. It actually enters the statement but the search criteria return null. The Search controller code is below:

if (isFromEditScreen != null && isFromEditScreen == true) {
        System.out.println("Inside isFromEditScreen ==== true");
        sYear = (String)request.getSession().getAttribute("sYearSave");
        collectionPeriod = (String)request.getSession().getAttribute("collectionPeriodSave");
        submission = (String)request.getSession().getAttribute("submissionSave");


        sYearSave = (String)request.getSession().getAttribute("sYearSave");
        collectionPeriodSave = (String)request.getSession().getAttribute("collectionPeriodSave");
        submissionSave = (String)request.getSession().getAttribute("submissionSave");

        System.out.println("sYearSave ==== " + sYearSave);
        System.out.println("submissionSave ==== " + submissionSave);
        System.out.println("collectionPeriodSave ==== " + collectionPeriodSave);
        System.out.println("isFromEditScreen set in else ==== " + isFromEditScreen);
        }

Any help would be greatly appreciated!

Richard
  • 826
  • 7
  • 23
  • 41
  • When you use the browser's back button, it doesn't generate a request to the server. Browsers usually loads the page from cache, did you try this wia a LinkButton or something like this? – rusty Mar 31 '13 at 17:44
  • 1
    And you're sure the values you put into the session are not `null`? Because, if you go back to the search screen and reload the page with a "blank" URL - i.e. there are no parameters given - you may end up overriding the values of the session, depending on your implementation of the Search controller. Aside from that - did you think about tabbed browsing? One session, two tabs, many problems... – skirsch Mar 31 '13 at 17:56
  • You should avoid passing search parameters from `session`. You should use [query strings](http://stackoverflow.com/questions/4128436/query-string-manipulation-in-java) for search parameters. – Apurv Mar 31 '13 at 18:01
  • @rusty I have a button on the page that goes back which reloads the page; the button doesn't using a history.back(); – Richard Mar 31 '13 at 19:32
  • @skirsch I'm sure because in my Edit Controller, the search values print out fine. I'm looking at the code and there isn't any overriding that I can see here. I've been doing prints everywhere. And no, I haven't thought of tabbed browsing. That won't be a big deal here. – Richard Mar 31 '13 at 19:34
  • @Apurv I do a getParameter to capture the values. I just need a solution to saving the user's search criteria as I move to and from the Edit and Search pages. – Richard Mar 31 '13 at 19:35
  • In your Search controller, is the part where you put the request parameters into the session defined before or after your `if (isFromEditScreen != null && isFromEditScreen == true) {...}` code fragment? – skirsch Mar 31 '13 at 21:11

2 Answers2

1

If you are using Spring MVC (as the question tag suggests), why not try using SessionAttributes annotaion and use the ModelAndView API from Spring? Also make sure the attribute name is unique across the application.

@RequestMapping("view-name")
@SessionAttributes( { "isFromEditScreen" })
public class YourController {

...

    @RequestMapping
    public ModelAndView display() {

        ModelAndView modelAndView = new ModelAndView("view-name");
        modelAndView.addObject("isFromEditScreen", new Boolean(true));
        return modelAndView;
    }

...
}
Taoufik Mohdit
  • 1,910
  • 3
  • 26
  • 39
  • We're not using this way of building out the controller. I wish we had started the project utilizing this method since it seems much more organized and versatile. – Richard Apr 01 '13 at 20:53
0

My session attributes were being overwritten. Overlooked mistake.

Richard
  • 826
  • 7
  • 23
  • 41