首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

ShowModalDialog 參數傳遞失敗解決方案:JSP 中轉頁面實現後台參數接收

     編輯:Java 相關知識

問題現象

使用 window.showModalDialog(url, ...) 打開頁面,URL 帶參數 ?acceptanceIds=xxx,但後台 Action 直接接收參數為 null。

根本原因

showModalDialog 打開的窗口無法直接將 URL 參數傳遞給後台 Action,需經過中間頁面中轉。

解決方案:JSP 中轉頁面 + iframe

1. 調用頁面修改

var url = "popub_acceptance.jsp?acceptanceIds=" + $("#acceptanceIds").val();
var returnValue = showModalDialog(url, 1000, 600);

2. 中轉頁面 popub_acceptance.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%
    String queryString = request.getQueryString(); // 獲取完整查詢字符串
%>
<html>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<table width="100%" border="0" height="100%">
<tr>
    <td>
        <iframe id="iframe_list" 
                src="loadAcceptance.action?<%=queryString%>" 
                frameborder="0" width="100%" height="100%" 
                scrolling="auto" align="center" valign="top">
        </iframe>
    </td>
</tr>
</table>
</body>
</html>

3. 後台 Action 保持不變

public String loadAcceptance() {
    if (StringUtils.isNotBlank(acceptanceIds)) {
        ascceptances = expenseService.loadAscceptances(acceptanceIds, getBmYear());
    }
    return "ascceptances_detail";
}

原理解析

  1. 主頁面調用 showModalDialog 打開中轉 JSP
  2. 中轉 JSP 通過 request.getQueryString() 獲取完整參數
  3. iframe 的 src 動態拼接參數請求真實 Action
  4. Action 正常接收參數,返回結果渲染在 iframe 中

現代化替代建議

showModalDialog 已被主流瀏覽器棄用/移除,建議遷移至:

  • Bootstrap Modal + AJAX 載入內容
  • Layer / SweetAlert2 等現代彈窗庫
  • React/Vue 模態組件(前端框架項目)

新方案天然支持參數傳遞、響應式、移動端適配,無需中轉頁面繞彎。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved