2013-01-08 23 views
4

打開另外一個窗口,我想我已經解決了一個問題,我發現這個解決方案稍早公佈:Javascript Post on Form Submit open a new window。但是,它並不完全符合我的要求,我無法弄清楚缺少的是什麼。需要有條件地從servlet的

這裏的問題...... 我有它一個表單的jsp。當表單被提交時(通過JavaScript),我希望父窗口總是重定向到另一個頁面。但是,根據用戶的輸入值,我還想CONDITIONALLY打開另一個窗口並將THAT窗口重定向到其他頁面。所以,總是重定向父,有時打開和重定向的孩子。

我在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。

我試過添加一個明確的重定向(「parentPage.jsp」);form.submit();,但這會產生競爭條件,並不是一個好的解決方案,因爲在提交表單之後不應調用任何內容。

我也嘗試修改servlet的內部邏輯,且僅當檢測到該條件重定向。但是,如果條件不存在,則會導致問題,因爲servlet不知道下一位向何處發送用戶。

有我的方式,以多種形式傳遞給servlet和通過Java處理呢?任何幫助將不勝感激。

-Bob

回答

3

我張貼的問題後,這出自己幾個小時,但希望確保該解決方案寫回之前的工作。

我認識到,按照我的邏輯,我總是會被重定向「父」頁面。因此,我不是試圖從原始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>