I feel dumb asking this question but I could not find a usable answer in the docs, forum or google.
I have been able to pass and receive a String between pages as follows:Code:
In page1FormController: ModelAndView mv = new ModelAndView(new RedirectView(quot;page2.htmquot;)); mv.addObject(quot;parmNamequot;, parmStringValue); return mv;
In page2FormController: String parmValue = request.getParameter(quot;parmNamequot;);
Following the same pattern for an object fails because getParameter() returns a String.
As a temporary solution I did the following. It works but it is ugly.
Code:
In page1FormController: request.getSession().setAttribute(quot;parmNamequot;, list);
In page2FormController: Listlt;myObjgt; list = (Listlt;myObjgt;)request.getSession().getAttribute(quot;parmNamequot;); request.getSession().removeAttribute(quot;parmNamequot;);
How can I pass and receive an object (specifically a Listlt;myClassgt;)?
Well I have a solution for you , Passing a string object parmName through your model and view can be done in a very efficient manner using JSTL and spring tag libraries.
For Case 1:
For the First case which you've mentioned .Just use the following code in your page2.jsp
lt;%@ page language=quot;javaquot; contentType=quot;text/html; charset=UTF-8quot; pageEncoding=quot;UTF-8quot;%gt;
lt;%@ taglib prefix=quot;cquot; uri=quot;jsp/jstl/corequot;%gt;
lt;%@ taglib prefix=quot;springquot; uri=quot;/springquot;%gt;
lt;!DOCTYPE html PUBLIC quot;-//W3C//DTD HTML 4.01 Transitional//ENquot; quot;TR/html4/loose.dtdquot;gt;
lt;htmlgt;
lt;headgt;
lt;meta from-equiv=quot;Content-Typequot; content=quot;text/html; charset=UTF-8quot;gt;
lt;titlegt;Insert title herelt;/titlegt;
lt;/headgt;
lt;bodygt;
lt;pgt;Here is the parmater from the model object: ${parmName}lt;/pgt;
lt;/bodygt;
lt;/htmlgt;
The trick is done by ${parmName} which is the name of the model object passed.This tag will retrieve your value using the taglibs marked in red and you will get output as :
Here is the parmater from the model object: quot;value of parmStringValuequot;.
Use this method rather than a request.getParameter as request.getparameter is really not recommended .
Case 2 is listed below in my next reply below.
Cheers,
Sushant
For case 2 :
Suppose you are passing a list in the model and view object like the following:
return new ModelAndView(getSuccessView(), quot;sampleListquot;,sampleList);
Now to retrieve this list in the next page,i.e. page 2.htm,Again you need JSTL tag-lib and you will need to use spring tag libraries for binding your object to POJOs.You can add the spring.tld library in your WEB-INF folder and add the following entry in your web.xml lt;jsp-configgt;
lt;taglibgt;
lt;taglib-urigt;/springlt;/taglib-urigt;
lt;taglib-locationgt;/WEB-INF/spring.tldlt;/taglib-locationgt;
lt;/taglibgt;
lt;/jsp-configgt;
Now all you have to do is import the spring tag library along with JSTL tag lib in your jsp.
Now coming to the JSP code to retrieve sampleList:
Code:
lt;c:forEach var=quot;sampleListquot; items=quot;${sampleList}quot;gt; lt;c ut value=quot;${sampleList.item1}quot;gt;lt;/c utgt;
lt;c ut value=quot;${sampleList.item2}quot;gt;lt;/c utgt;
lt;/c:forEachgt;So for example suppose I have a list of Song objects containing title and artist.
Like ArrayListlt;Songgt; sampleList = new ArrayListlt;Songgt;();
so for this list you can use the following code :
Code:
lt;c:forEach var=quot;sampleListquot; items=quot;${sampleList}quot;gt; lt;c ut value=quot;${sampleList.title}quot;gt;lt;/c utgt;
lt;c ut value=quot;${sampleList.artist}quot;gt;lt;/c utgt;
lt;/c:forEachgt;
I hope this helps in resolving your query and gives you a clear undertanding of how you can use Model and view object passed into pages.
Cheers,
Sushant
Hi,
Sushant, thanks for your examples, but I'm afraid I think you answered a different question to the one asked. If I interpret bmelloni's post correctly, this is specifically about how to set up some object in a method of Controller A, then redirect to pass control to a method of Controller B - and still be able to access that object in Controller B.
I've spent several hours looking at the same problem.I have a screen with a list of Product search results, where I can select a set of products and press a Modify button. Now, to avoid coupling my Search view/form to my Modify controller, I have a method on my Search controller to accept the form; I then would like to redirect to the Modify controller so it can go and fetch all the Product data and render its Product view. Unfortunately, upon redirect, anything put into the model is either turned into RequestParameters (if simple String, int etc) or ignored, so I don't have access to them in the second (Modify) controller.
@bmelloni: in the absence of anything more elegant, I'm about to try using request.setAttribute() and getAttribute() - defined by the ServletRequest interface. This should be slightly simpler than your Session attributes as no need to remove them explicitly.
Update: using get/setRequestAttribute works, provided you use the quot;forward:quot; prefix, not the quot;redirect:quot; one.
(I'm using the annotation-based controllers in Spring MVC 2.5, so this might look a little different for you.)
First controller:
Code:
@RequestMapping(quot;/search/modify.htmquot;)
public String modify(ModelMap model, fromServletRequest request) { // ...other stuff... DummyModifyForm dummyForm = new DummyModifyForm(quot;testingquot;); request.setAttribute(quot;dummyModifyFormquot;); return quot;forward:/modify/dummy.htmquot;;
}
Second controller:
Code:
@RequestMapping(quot;/modify/dummy.htmquot;)
public String dummy(ModelMap model, fromServletRequest request) { DummyModifyForm d = (DummyModifyForm) request.getAttribute(quot;dummyModifyFormquot;); // Breakpoint here and debugger shows d contains DummyModifyForm with quot;testingquot; // When I changed to use redirect: d was null
}
Still, it seems pretty hacky given that the Spring MVC idiom is to avoid passing in the request if at all possible. Is ours really such an unusual requirement?
Edit: crossposting!
Originally Posted by michaelbannisterHi,
@bmelloni: in the absence of anything more elegant, I'm about to try using request.setAttribute() and getAttribute() - defined by the ServletRequest interface. This should be slightly simpler than your Session attributes as no need to remove them explicitly.
Thanks Michael, I should have thought of that.
Still, I still suspect that there is a SpringMVC way of doing the same thing. It is a very basic need and the solution must already exist in the framework.
It seems there's a JIRA ticket for this: SPR-1068
That means there probably isn't a more elegant way to do this at the moment.
Doesn't sound like the same question either, but...
How are you putting data into the session?
How are you then trying to retrieve it?
Some small code samples might help.
laserlipo, I might be misinterpreting, but from what you wrote it looks like you are thinking of the data in the command object or the model as if it was in the session. It isn't, and it only persist as long as you return don't leave the page.
What we are talking about here is ways of passing the data between pages.
So far, the code snippet from Michael seems to be the best way to pass the data. Or, if you need data that stays around longer look at my second code snippet - the data will stick around in the fromSession until you call the removeAttribute() method (if you ever do).
In both cases, the code to move the data into the fromSession or fromRequest and back is in the controller. It is up to us (the developers) to write it, so that we can choose what to pass between pages and what not to pass.
The Jira ticket Michael found would solve the problem in a cleaner way, but seems to be targeted for solution in Spring 3.0.
I hope this clarifies things for you. |