Spring MVC - passing variables from one page to anther

Aravind Datta picture Aravind Datta · Jun 3, 2012 · Viewed 23.2k times · Source

I need help. I am working on a project where I have multiple pages and multiple forms; each page has one form. I just need to be able to pass values from one jsp to another. What should I do?

I am new to Spring MVC. I am using spring 2.5.6.

Here's my design:

formPage1.jsp --> Controller1 --> formPage2a.jsp --> Controller2 needs val frm pg1 & pg2a. formPage1.jsp --> Controller1 --> formPage2b.jsp --> Controller3 needs val frm pg1 & pg2b. formPage1.jsp --> Controller1 --> formPage2c.jsp --> Controller4 needs val frm pg1 & pg2c.

As you can see above, formPage1.jsp can load either formPage2a, formPage2b or formPage2c. Based on the input provided in formPage1.jsp, it goes to the controller (which is an extension of SimpleFormController) and controller get the values entered by user = command object.

I want to be able to use these command object values in either formPage2a, formPage2b or formPage2c when they are submitted to another controller.

here's the current code:


formPage1.jsp:

<form:form method="post" commandName="gainLossRequest">
            <form:errors path="*" cssClass="error"/>
            <table>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td><h4>Choose Client</h4></td>
                                <td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <form:select path="client">
                            <form:option value="none" label="Select" />
                            <form:option value="abc" label="abc" />
                            <form:option value="def" label="def" />
                            <form:option value="xyz" label="xyz" />
                        </form:select>
                    </td>
                </tr>
<tr>
                    <td colspan="2">
                        <input type="reset" value="Reset" />
                        <input type="submit" value="Next" />
                    </td>
                </tr>
            </table>
            </form:form>

Controller1.java

public class TestController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public TestController() {
        logger.info("entering TestController.constructor..");
        setCommandClass(UserPreference.class);
        setCommandName("userPreference");
    }

    public ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws ServletException {
        logger.info("entering TestController.onSubmit all..");

        UserPreference userPreference = (UserPreference) command;

        ModelAndView view = null;

        if ("abc".equals(userPreference.getClient())) {
            GainLossRequest gainLossRequest = new GainLossRequest(userPreference);
            view = new ModelAndView("redirect:/test/gainLossRequest.htm",
                    "gainLossRequest", gainLossRequest);
                } else if ("def".equals(userPreference.getClient())) {
            IncomingPositionsRequest incomingPositionsRequest = new IncomingPositionsRequest();
            view = new ModelAndView(
                    "redirect:/test/incomingPositionsRequest.htm",
                    "incomingPositionsRequest", incomingPositionsRequest);
        } else if ("xyz".equals(userPreference
                .getClient())) {
            TaxStrategyRequest taxStrategyRequest = new TaxStrategyRequest();
            view = new ModelAndView("redirect:/test/taxStrategyRequest.htm",
                    "taxStrategyRequest", taxStrategyRequest);
        }
        }
}

formPage2a.jsp

<form:form method="post" commandName="gainLossRequest">
            <form:errors path="*" cssClass="error"/>
            <table style="width: 60%">
                <tr>
                     <td>Account Number (s):</td>
                     <td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
                </tr>
                <tr>
                    <td>
                        User Chosen Client: 
                    </td>
                    <td>
                        <c:out value="${gainLossRequest.client}"/>
                    </td>
                </tr>
                                <tr colspan="2">
                                        <td>
                        <input type="reset" value="Reset" />
                        <input type="submit" value="Submit" />
                    </td>
                </tr>

dispatcher servlet config

<!-- setupNew.jsp is the first jsp --> 

<bean name="/test/setupNew.htm" class="chimeraweb.web.TestController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="userPreference"/>
        <property name="commandClass" value="chimeraweb.service.UserPreference"/>
        <property name="validator">
            <bean class="chimeraweb.service.UserPreferenceValidator"/>
        </property>
        <property name="formView" value="/test/setupNew"/>
    </bean>


<!-- gainLossRequest.jsp is the 2nd jsp where I want to display the values captured in the first jsp page -->

    <bean name="/test/gainLossRequest.htm" class="chimeraweb.web.SimpleGainLossController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="gainLossRequest"/>
        <property name="commandClass" value="chimeraweb.service.GainLossRequest"/>
        <property name="validator">
            <bean class="chimeraweb.service.GainLossValidator"/>
        </property>
        <property name="formView" value="/test/gainLossRequest"/>
    </bean>

Please help!!

Answer

abehrens picture abehrens · Jun 4, 2012

You could also use session attributes, either manually, such as:

public ModelAndView handleRequest(HttpServletRequest request){
      request.getSession().getAttribute("nameOfAttribute");
}

Apologies for writing this as an annotated controller, I do not recall if xml config controllers offer this feature...

There is no Spring code involved for that. The other option is to use the @SessionAttribute annotation on your controller:

@Controller 
@SessionAttributes("nameOfAttribute")
public class MyController{
//your session attribute can be accessed in controller methods using @ModelAttribute
public ModelAndView handleRequest(@ModelAttribute("nameOfAttribute")){
      session.getAttribute("nameOfAttribute");
}

Note

You will need to clear the session attribute when you are done with it:

request.getSession().setAttribute("nameOfAttribute", null);