【问题标题】:Spring MVC - passing variables from one page to antherSpring MVC - 将变量从一个页面传递到另一个页面
【发布时间】:2012-06-03 15:54:01
【问题描述】:

我需要帮助。我正在做一个有多个页面和多个表单的项目;每个页面都有一个表格。我只需要能够将值从一个 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>

请帮忙!!

【问题讨论】:

  • 问题是.. 我无法将值从第一个 formPage 传递到第二个 formPage。我想知道该怎么做。
  • "没有人可以帮助我吗?"。请尽量友善,您不会在这里付钱给任何人帮助您。

标签: java spring-mvc tld


【解决方案1】:

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

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);

【讨论】:

  • 我能够访问用户在第一个 jsp 页面中输入的值 谢谢。我没有完全明白。请告诉我这个假设是否错误。第 1 步:用户在第 1 个 jsp 中输入值 第 2 步:值转到 controller1.java 第 3 步:我需要能够在第 2 个 jsp 中捕获相同的值 第 4 步:将相同的值传递给 controller2.java。我成功完成了第 1 步和第 2 步。因此,在控制器 1 中获取用户输入的值不是问题。我想以某种方式将这些值传递给 controller2.java(步骤 4)。但我知道如果不进入第 3 步,我将无法进入第 4 步。有意义吗?
  • 是的,我的意思是您可以将您正在处理的对象存储在用户的会话中。这样就不需要在其中放置所有数据的隐藏输入。不要将变量传递给 jsp,而是将您正在构建的任何域对象放入 Session 对象中。我相信 Spring 2.5 有一个多步操作控制器或向导控制器,您也可以使用它来完成此操作。
【解决方案2】:

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

给你的伪代码。

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

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

【讨论】:

  • 好的,谢谢。这就是我在 Controller1.java 中所做的。我想知道“如何设置第二种形式的值”??此代码是否有助于设置表单中的值?还是错了?? 2nd.jsp 中的命令对象是 GainLossRequest。我正在将我从 1st.jsp 获得的值设置为这个对象.. 这样我就可以在 2nd.jsp 中检索它们。但是当我尝试做.. 时,它什么也不显示。
  • ----------------- Controller1.java 内部: ------------------ 字符串client = ((UserPreference) command).getClient(); logger.info("用户选择的客户端:" + client); GainLossRequest gainLossRequest = new GainLossRequest(userPreference); view = new ModelAndView("redirect:/test/gainLossRequest.htm", "gainLossRequest", gainLossRequest); logger.info("分配给glRequest对象后控制器内部客户端的值:" + gainLossRequest.getClient());
  • 你需要查看你的Controller1.java。您正在根据某些条件返回不同的模型对象。您需要将它们保留在这些模型对象中。我有一段时间没有使用xml配置了。所以请根据您的判断来判断需要做什么。
  • 您正在使用重定向:对于您的观点,这将导致您的请求丢失对域对象的任何知识。 jsp 不知道用什么填充隐藏的输入,因为该对象的实例在页面中不可用。
  • 嗯..好的。这非常有用。谢谢。我想我正在慢慢到达那里..但还没有完全到达那里。我必须重定向的原因是因为我想将不同的 URL 映射到不同的 bean。基本上,根据用户选择的“客户端”,我想将他带到不同的页面。这一行帮助我实现了这一点——。所以,如果我摆脱这个重定向:前缀,那么,我将无法根据输入加载页面。或者可能是,这就是我所知道的。有输入吗?
【解决方案3】:

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

【讨论】:

  • 感谢您的快速回复。真的很感激。但我不知道如何将变量作为隐藏传递。你知道语法吗?
  • 我试过了。它没有用。我在第二个 jsp 中更改了这样的代码。 客户端:
  • 如果唯一的症状是“它不起作用”,就不可能为您提供更多帮助。更精确。告诉我们它做了什么以及应该做什么。向我们展示您的代码。给我们异常堆栈跟踪。 “它不起作用”没有任何意义。当你去看医生时,你不只是说:“我病了”。你告诉他你的头疼,你呕吐等等。
  • 对不起。我以为我已经够清楚了。我粘贴了关于这两个 jsp 页面和中间控制器的所有代码。问题是,用户在 page1 中输入了一个值,我想在 page2 中捕获它。我无法做到这一点。我添加了丢失的 DispatcherServlet 配置文件。您需要更多信息吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 2019-03-06
相关资源
最近更新 更多