Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC passing variables from one jsp page to another jsp page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC passing variables from one jsp page to another jsp page

    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>

    ---------------------------------------------

    Please help!!

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable.

    You want to checkout Spring Web Flow which is made for these kind of requirements else use the AbstractWizardFormController although I strongly recommend Spring Web Flow.

    Comment

    Working...
    X