4

我需要帮助。我正在做一个有多个页面和多个表单的项目;每个页面都有一个表格。我只需要能够将值从一个 jsp 传递到另一个。我应该怎么办?

我是 Spring MVC 的新手。我正在使用弹簧 2.5.6。

这是我的设计:

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

正如您在上面看到的,formPage1.jsp 可以加载 formPage2a、formPage2b 或 formPage2c。根据 formPage1.jsp 中提供的输入,它进入控制器(SimpleFormController 的扩展),控制器获取 user = command 对象输入的值。

当它们被提交到另一个控制器时,我希望能够在 formPage2a、formPage2b 或 formPage2c 中使用这些命令对象值。

这是当前代码:


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>

调度员 servlet 配置

<!-- 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>

请帮忙!!

4

3 回答 3

7

您还可以手动使用会话属性,例如:

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

很抱歉将其编写为带注释的控制器,我不记得 xml 配置控制器是否提供此功能...

没有涉及到的 Spring 代码。另一种选择是在控制器上使用 @SessionAttribute 注释:

@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");
}

笔记

完成后,您需要清除会话属性:

request.getSession().setAttribute("nameOfAttribute", null);
于 2012-06-04T13:23:20.840 回答
2

您必须使用 JB Nizet 上面提到的隐藏变量来保留用户在第一页上输入的信息。或者,您可以在相应控制器上返回的模型属性中设置值。

给你的伪代码。

formPage1.jsp --> Controller1 --> 通过从请求对象中检索它来设置此表单中的值 --> formPage2a.jsp --> Controller2 将具有 val frm pg1 和 pg2a。

这样就不需要维护会话属性。

于 2012-06-04T13:56:35.117 回答
1

最简单的方法是使用第二页中的隐藏字段来存储用户在第一个表单中输入的内容。这样,所有字段,包括第一页中的字段,都将与第二个表单一起提交。

于 2012-06-03T16:07:08.890 回答