【问题标题】:Data transfer within web flow using links in Spring web-flow使用 Spring web-flow 中的链接在 web 流中传输数据
【发布时间】:2013-11-02 16:49:33
【问题描述】:

我正在尝试创建 Spring Web 流应用程序。我有以下情况:

flow1.xml

<var name="user" class="com.test.User" />

<view-state id="signup" model="user">
    <transition on="loginCredentialsEntered" to="lookupUser" />
</view-state>

 <decision-state id="lookupUser">
    <if test="myActionBean.lookupUser(user)"
        then="userRegistered" else="registerUser" />
</decision-state>

signup.jsp:

<html xmlns:form="http://www.springframework.org/tags/form">
<body>
    <form action="&_eventId=loginCredentialsEntered" method="post"> 
        <input type="hidden" name="_flowExecutionKey" value=""/>
        <table>
            <tr>
                <td>Login Name:</td>
                <td><input type="text" name="loginName"/></td>
            </tr>
         </table>
        <input type="submit" value="Login" />
    </form>
</body>
</html>

在 myActionBean.lookupUser(user) 方法中,我可以看到正在传递的 user.loginName 是在表单中输入的。

但是,如果我使用链接而不是按钮来提交表单

<a href="${flowExecutionUrl}&_eventId=loginCredentialsEntered">Login</a>

我可以看到 user.loginName 为空。

谁能解释一下原因和解决方法

提前致谢

【问题讨论】:

    标签: java spring jsp spring-webflow


    【解决方案1】:

    那是因为表单没有提交到任何地方。
    Form 标签更改为:

    <form action="${flowExecutionUrl}&_eventId=loginCredentialsEntered" 
                                                             method="post"> 
    

    它会起作用的。

    顺便说一句,你不应该像这样使用 URL 传递参数,最好和 _flowExecutionKey 做同样的事情:

    <form action="${flowExecutionUrl}" method="post"> 
    <input type="hidden" name="_eventId" value="loginCredentialsEntered" /> 
    ...
    

    【讨论】:

    • 亲爱的阿尔法辛,感谢您的回复。
    • 亲爱的阿尔法辛,感谢您的回复。我想将域对象绑定到表单。因此,我将整个对象传递给 myActionBean.lookupUser(user)。无论如何,现在表单如下所示: '
      Login
      ' 但它没有帮助。 user.email 在 myActionBean.lookupUser() 方法中仍然为空
    【解决方案2】:

    如果您将按钮替换为锚点,那么您就不再提交表单了吗?点击链接不会发送loginName 参数 - 因为它没有提交表单。

    如果您想用链接替换按钮,那么您需要在单击链接时使用 Javascript 提交表单。

    给表格起个名字:

    <form name="myForm" action="${flowExecutionUrl}&_eventId=loginCredentialsEntered" 
                                                             method="post"> 
    

    并将链接修改为:

    <a href="#" onclick="document.myForm.submit();">Login</a>
    

    另外,更好的解决方案是保留按钮并使用 CSS 将其样式设置为更像链接。那么就不需要Javascript了。

    【讨论】:

    • 这个技巧奏效了,非常感谢。我正在使用该链接,因为我的 HTML 开发人员设计了该页面以使该链接看起来像一个按钮。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多