PDA

View Full Version : how to persist values while switching from one controller to the othe


ankit_agarwal
Apr 26th, 2007, 01:31 AM
i am using two simple form controller and i am switching from one controller to other using a hyperlink which direct to the other controller....Is there any way that what ever the values i have in my form bean object i can pass to the second controller using post method and from second controller to the first controller basically i want to preserve some of my value which i will get once in the first controller

YNight
Apr 26th, 2007, 10:15 AM
Can you post some code to illustrate your desire please? That makes it easier to help :)

ankit_agarwal
Apr 27th, 2007, 01:35 AM
this is the first controller
public class Mycontroller extends SimpleFormController{
public Mycontroller() {
// TODO Auto-generated constructor stub
setCommandClass(MyForm.class);
setCommandName("myForm");
setFormView("page1");
setSuccessView("page1");
}

@Override
protected Map referenceData(HttpServletRequest req, Object arg1, Errors arg2) throws Exception {
Map map=new HashMap();
MyForm myForm=(MyForm) arg1;
myForm.setName("ankit");
map.put("myForm", myForm);
return map;

}
}



public class MyNewController extends SimpleFormController{
public MyNewController() {
// TODO Auto-generated constructor stub
setCommandClass(MyForm.class);
setCommandName("myForm");
setFormView("page2");
setSuccessView("page2");
}

protected Map referenceData(HttpServletRequest req, Object arg1, Errors arg2) throws Exception {
Map map=new HashMap();
MyForm mynewForm=(MyForm) arg1;
map.put("myForm", bb);
return map;

}
}


public class MyForm {

String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}

now i have one page1.jsp where i have a button or a hyperlink which ever is convinient for you.... i will click on that and it will direct me to the second controller so how will i get the value that i have setted in first controller... i hope i m clear with my requirement.....one thing more i donot want to use session... iwant spring to do it for me

fox
Apr 27th, 2007, 03:04 AM
the map you use in referenceData is only for processing pages by the given controller in a given request.

if you don't want to use session, you can use something else - database, files or you can pass your data in the hyperlink as a parameters but you have to do something, it will not make your data automagically visible to every controller :)

Cheers,
fox

edit:

anyway i've looked more closely on what are you trying to do and i think you are making a wizard (page1, page2...)
for that there is a nice controller: AbstractWizardFormController (http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/AbstractWizardFormController.html)

ankit_agarwal
Apr 27th, 2007, 03:11 AM
thanks i got it what you mean