当前位置: 首页 > 面试题库 >

Spring MVC-将变量从一页传递到另一个

步博厚
2023-03-14
问题内容

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

我是Spring MVC的新手。我正在使用Spring 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
=命令对象输入的值。

我希望能够将这些命令对象值提交给另一个控制器时在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>

请帮忙!!


问题答案:

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

您的伪代码。

formPage1.jsp-> Controller1->通过从Request对象中获取它来设置此格式的值-> formPage2a.jsp->
Controller2的val frm分别为pg1和pg2a。

这样,无需维护会话属性。



 类似资料:
  • 我有一个来自firebase的uid,但它是这样存储的: checkout.js 存储我需要传递到另一个页面的uid。这个uid存储在中,我需要将它放到中才能在那里使用uid。我该怎么做?如果需要更多代码,请让我知道。我正在使用react/js

  • 问题内容: 我有两个页面-“页面1”和“页面2”。在第1页上,有一个文本框,其值例如为100,最后是一个按钮。 通过按下按钮,我希望javascript将文本框的值保存在全局(?)变量中,然后跳转到第2页。使用“ window.onload”,我想要第二个Javascript函数来提醒保存在page1的值。 这是我的Javascript代码: 在“页面1”上,我有一个带有以下内容的发送按钮: 它启

  • 问题内容: 我有两个 newAccessLevels.java ,它有两个按钮“ Level 1”,“ Level 2”和 newAccessPanel.java。 我需要获取用户选择“ 1或2”的级别,以便可以在标题中显示它。 accessPanel.java, 例如访问级别1,访问级别2。如何完成此操作。下面是示例代码,因此,如果单击级别1,则将打开标题为* ACCESS LEVEL 1 的n

  • 问题内容: 看起来很简单,但是我找不到很好的方法。 在第一页中说,我创建了一个变量 该页面的表单操作是“ Page2.php”。因此,在Page2.php中,如何访问该变量?我知道我可以在会话中做到这一点,但我认为对于一个简单的字符串来说这太多了,我只需要传递一个简单的字符串(文件名)即可。 我该如何实现? 谢谢! 问题答案: HTML / HTTP是无状态的,换句话说,您在上一页所做的/与当前页

  • 问题内容: 我刚开始使用快速语言,并且知道这个问题是重复的。我发现了几个类似的问题和答案,但我无法弄清这个问题。 我想将ScandString变量的值从ScanViewController传递给ResultViewController。 ScanViewcontroller如下: ResultViewController如下: println(detectedString)没有给我任何结果。问题是

  • 问题内容: 我正在使用类似的会话将变量传递到URL中的另一个页面,但是看来我无法将另一个变量连接到相同的URL并成功在下一页中检索它 第1页 第2页 在第2页上显示错误,但如果仅在此处使用 效果很好,但是我需要能够在url中使用两个变量,以便页面2可以检索得到它们。 问题答案: 使用与号将变量粘合在一起:

  • 问题内容: 我想将一个类变量传递给另一个类,并使其成为该类的类变量。在以下情况下我该怎么做? 问题答案: 很难理解您正在问的问题,但这是一个可能的答案: 使B类成为A的子类: 如果重新声明为,则会出现一种情况,其中存在一个可以称为或的属性。(或在任一或刚刚…甚至作为或地方,并有类型和分别。) 如果您无法在和(或,以及一些包含声明的第三类)之间创建直接或子类型关系,那么您很不走运。他们无法共享声明。

  • 我的视图文件夹中有两个EJ,我创建了非常简单的EJ,看看是否可以将变量从一个EJ发送到另一个EJ。 a、 vews文件中的ejs b.ejs有 在我的节点js这是什么我做const表达式=要求('Express'); const body Parser=要求('body-parser'); 我想邮报必须在这里做点什么...