javascript - 需要有条件地从 servlet 打开一个额外的窗口

标签 javascript forms servlets redirect window

在找到这个解决方案后,我以为我已经解决了我之前发布的问题:Javascript Post on Form Submit open a new window .但是,它并不能完全满足我的要求,而且我无法弄清楚缺少什么。

问题来了... 我有一个带有表单的jsp。提交表单时(通过 JavaScript),我希望父窗口始终重定向到另一个页面。但是,根据用户的输入值,我也有条件地想打开另一个窗口并将该窗口重定向到另一个页面。因此,始终重定向父级,有时打开并重定向子级。

我在 jsp 中的代码:

<script>
  function handleFormSubmit() {
    var form = document.getElementById("myForm");
    ...
    if (checkbox.checked) {
      // this code creates the child window, but ONLY if the checkbox is checked
      myForm.setAttribute("target", "newResultPage");
      window.open("childpage.jsp", "newResultPage", "width=850,height=550,...");
    }

    form.submit();
  }
</script>

这是 html 表单:

<form id='myForm' method='post' action='someAction' accept-charset='UTF-8'>
 // some hidden input elements here
</form>

Servlet 逻辑:

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String redirectedPage = "/parentPage";

    if (someCondition) {
      redirectedPage = "/childpage.jsp";
    }

    RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher(redirectedPage);
    reqDispatcher.forward(request,response);
  }
}

当不满足条件时,一切正常。 servlet 被调用,调用页面被重定向到 parentPage。但是,如果满足条件,servlet 会在新的子窗口中正确打开 childpage.jsp,但不会将调用者页面重定向到 parentPage。

我尝试在 form.submit(); 之后添加显式 redirect("parentPage.jsp"); ,但这会造成竞争条件并且不是一个好的解决方案,因为在提交表单后不应调用任何内容。

我还尝试修改 servlet 内部的逻辑,并且仅在检测到条件时才重定向。但是,当条件不存在时,这会导致问题,因为 servlet 不知道接下来将用户发送到哪里。

有没有办法将多个表单传递给 servlet 并通过 Java 处理它?任何帮助将不胜感激。

-鲍勃

最佳答案

我在发布问题几个小时后自己想出了这个问题,但想在回信之前确保解决方案有效。

我认识到,根据我的逻辑,我总是会重定向“父”页面。因此,我没有尝试从原始 servlet 打开子窗口,而是将该逻辑移至父页面并使用 onload 函数(有条件地)启动它。这是更新后的代码以澄清:

更新的 jsp 代码:

<script>
  function handleFormSubmit() {
    var form = document.getElementById("myForm");
    ...
    if (checkbox.checked) {
      // we pass an additional parameter that will make its way to the new parent page
      myForm.setAttribute("launchNow", "1");

      /*
       * this code no longer applies
       */
      // myForm.setAttribute("target", "newResultPage");
      // window.open("childpage.jsp", "newResultPage", "width=850,height=550,...");
    }

    form.submit();
  }
</script>

Html 表单代码(没变!):

<form id='myForm' method='post' action='someAction' accept-charset='UTF-8'>
 // some hidden input elements here
</form>

更新的 servlet 逻辑:

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String redirectedPage = "/parentPage";

    String launchNow = request.getParameter("launchNow");

    if (someCondition) {

      /*
       * Rather than redirecting, we pass an additional params to the new parent page
       */
      // redirectedPage = "/childpage.jsp";

       request.setAttribute("launchNow", launchNow);
       request.setAttribute("anotherParam", someValue);
    }

    RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher(redirectedPage);
    reqDispatcher.forward(request,response);
  }
}

最后,在新的父 jsp 页面中,我们检查附加参数并采取相应措施:

<html>
  <head></head>

  <%
    String launchNow    = request.getParameter( "launchNow" );
    String anotherParam = request.getParameter( "anotherParam" );
  %>

  <body onload="loadOnLaunch(<%= launchNow %>)">
    <form id="hiddenForm" method="post" target="newResultPage" action="anotherAction">
      <input id='anotherParam' type='hidden' name='anotherParam' value='<%= anotherParam %>'/>
    </form>

    <script type='text/javascript'>

      function loadOnLaunch(launchNow) {
        if (launchNow != null && launchNow == "1") {

          /*
           * NOTE: that this block used to reside in the original parent form!
           * It stayed the same, but the action getting called is different
           * thus invoking a different servlet
           */
          var myForm = document.getElementById("hiddenForm");
          myForm.setAttribute("target", "newResultPage");
          window.open("childpage.jsp", "newResultPage", "width=850,height=550,...");
        }
      }

    </script>
    ...
  </body>
</html>

关于javascript - 需要有条件地从 servlet 打开一个额外的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14220315/

相关文章:

javascript - 无法将数据从操作传递到 reducer

java - 显示标签问题

spring - 在 Spring Boot 中将 servlet 添加到管理端口

javascript - 如何将 jwt token 应用于生成的 Angular 2 JS 文件?

javascript - umi 请求拦截器响应的刷新 Token

python - 使用 Python 以 Mechanize 的形式设置 Select 控件

python - 使用模板自定义 Django FormWizard 步骤

.net - Visual C++ 应用程序作为 MS Access 数据库的前端 - 多用户数据访问和类似 Canvas 的 GUI 问题

java - 如何在启动期间使用参数初始化 Servlet?

javascript - jQuery Ajax 调用未找到元素问题