1

I need help with a very simple problem I have in my web app using Spring MVC. controller1 retrieves a list of objects from a database that are displayed in jsp1. In my jsp1, each object is represented by a link that redirects to controller2. I want to have the object represented by that link in controller2 (or the full list of objects).

These are controller 1 and controller 2:

@RequestMapping(value = "/mapping1", method = RequestMethod.GET)
public String controller1(Map<String, Object> map, HttpServletRequest request) {
    List testList = service.getList(request.getParameter("testParam"))
    map.put("testList", testList);
    return "jsp1";
}

@RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public String controller2(Map<String, Object> map) {
    // How to get testObject (or testList) here?
    return "jsp2";
}

And this is jsp1:

<c:forEach items="${testList}" var="testObject">
    <tr><a href="/mapping2">${testObject.name}</a></tr>
</c:forEach>
danst_18
  • 497
  • 1
  • 5
  • 17
  • Use session or flash attributes. – Sotirios Delimanolis Mar 04 '14 at 18:59
  • As far as I know I can only pass parameters through the session, and I need to pass whole objects – danst_18 Mar 04 '14 at 19:02
  • 1
    Look at the method `HttpSession#setAttribute(String, Object)`. You can very much store any object you want. – Sotirios Delimanolis Mar 04 '14 at 19:04
  • Thanks. I was mistaken then, I think I read somewhere that objects coulnd't be passed through the session. I also took a look to flash attributes. How would I use them in this situation? Any advantages of using that instead of the session? – danst_18 Mar 04 '14 at 19:09
  • Flash attributes are actually session attributes that get removed after the next request. I don't really understand your use case because it seems like you can just call your service method again, but otherwise, just add the attributes and then retrieve them. – Sotirios Delimanolis Mar 04 '14 at 19:11
  • Yes, I thought about that also. But I was thinking that calling the service method and therefore accesing the database again would be slower than storing that object in memory. Maybe I am wrong. Sorry I'm quite a begginner. – danst_18 Mar 04 '14 at 19:15
  • It depends. Test both, see what works best. – Sotirios Delimanolis Mar 04 '14 at 19:16
  • @danst_18 ofcourse it takes a little time but if you see the other aspect keeping whole object in the session attribute doesn't take memory. So you have actually think about it on what side you want to compermise depending upon your requirement. You have to see on both aspects memory as well as response time. – Ravi Kumar Mar 05 '14 at 06:45
  • chek this out: http://stackoverflow.com/questions/7429649/how-to-pass-model-attributes-from-one-spring-mvc-controller-to-another-controlle – Arun Jan 06 '15 at 12:58

0 Answers0