我认为在找到此解决方案之后我已经解决了我之前发布的问题:Javascript Post on Form Submit open a new window。然而,它并不是我想要的,我无法弄清楚缺少什么。
这是问题...... 我有一个带有表单的jsp。提交表单(通过JavaScript)我希望父窗口始终重定向到另一个页面。但是,根据用户的输入值,我还有条件地想要打开另一个窗口并将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。
我尝试在 form.submit(); 之后添加一个明确的 重定向(“parentPage.jsp”); ,但这会产生竞争条件并且不是一个好的解决方案,因为在提交表单后不应该调用任何内容。
我也尝试修改servlet中的逻辑,并且仅在检测到条件时重定向。但是,当条件不存在时,这会导致问题,因为servlet不知道下一步将用户发送到何处。
有没有办法让我将多个表单传递给servlet并通过Java处理它?任何帮助将不胜感激。
-Bob
答案 0 :(得分: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>