【问题标题】:URL redirection code in jsp is not workingjsp中的URL重定向代码不起作用
【发布时间】:2013-11-20 06:39:24
【问题描述】:

我想在几秒钟后从一个 jsp 重定向到另一个 jsp。我有以下代码:

<body style="height: 302px; color: Olive">
    <h2> Result of your transaction </h2>
    <hr style="height: 5px; ">
    <p align="center">
    <h3> ${result}</h3>
    <br>
    <hr> 
    <form id="clientCall" method="post" action="http://localhost:8080/SpringMVCClient/transactionResultMovieTheater.htm?">
        <input type="hidden" name="userId" id="userId" value="${userId}"/>
    </form>
    <script type="text/javascript">
        document.setTimeout("alert ('Now you will be redirected to the main Site');",3000);
        document.getElementById("clientCall").submit();
    </script>
</body>

但是什么都没有发生。它只是打印出 ${result},然后停留在同一页面上。我写错代码了吗?请在这里指导我。

提前致谢!

【问题讨论】:

    标签: html jsp timeout response.redirect


    【解决方案1】:

    form 标签的action 属性在input typesubmitbutton 时使用。

    在您的情况下,输入类型是hidden,我认为这会在您的代码中产生问题。尝试使用另一种输入类型 submit

    【讨论】:

    • 隐藏输入类型用于将值发送回主站点。我不想让用户知道这个值。
    • 那么另一种解决方案是当您使用 jsp 时,为什么不只在服务器端使用 jsp 进行重定向。就像表单提交帖子数据然后在 jsp scriptlet 中一样,使用 response.sendRedirect(url) 之类的方法。
    • 我应该如何编码?我还需要将 userId 发送回客户端。
    • 当您使用scriptlet 来jsp 代码如 时,包含的代码将在服务器端执行。因此,当说 page1.jsp 将发布数据发送到服务器时,相同的 jsp 代码将在服务器端执行。因此,在 jsp scriptlet 中编写重定向逻辑还将来自 page1.jsp 的发布数据发送到新的重定向页面。您需要研究如何在 jsp 中将数据从一个页面发送到另一个页面。
    • 如果所有页面都位于同一台服务器上,则可以使用 forward() 方法进行 POST 请求。参考它可能会有所帮助 - stackoverflow.com/questions/14039121/…
    【解决方案2】:

    而不是上面代码中的document.getElementById("clientCall").submit();

    我用过

    window.open("http://localhost:8080/SpringMVCClient/transactionResultMovieTheater.htm?userId="+"${userId}", "_self");

    效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2012-06-15
      • 2014-08-30
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多