承接上篇,繼續分享 Struts 專案實戰踩坑筆記,涵蓋前端細節、部署陷阱、效能優化等 6 大主題。
showModalDialog() 無 title 參數,title 需在被開啟的 JSP <head> 內第一行寫 <TITLE>。普通視窗容忍位置錯誤,模態視窗極敏感。
常見 5 大原因:
<html:form> 之外使用表單子標籤taglib prefix 不為 htmlproperty 未對應 FormBean 屬性<html:form> 而非 <form>jboss-4.0.5.GA/server/default/lib/method: "get" 受 URL 長度限制,大表單請求會失敗無回應。改 method: "post" 即解決。
公共專案 JSP 內用 <%@ page import="..."%> 引用 Servlet 類別,其他專案 include 該 JSP 時找不到類別。最佳解:公共類別打 JAR,放各專案 WEB-INF/lib 或 Server 共用 lib。
| 方式 | 適用場景 | 範例 |
|---|---|---|
Struts <logic:equal> | 變數在 request/session 等範圍 | <logic:equal name="JSP_USER_CODE" value="12345">...</logic:equal> |
JSTL <c:if> | 依 URL 參數判斷 | <c:if test="${param.modual=='select_users'}">...</c:if> |
Scriptlet <% %> | 複雜 Java 邏輯 | <% if(...) {%>...<%}else{%>...<%}%> |
彈出新視窗存取開啟者元素用 self.opener,非 self.parent(parent/child 用於框架/多視窗架構)。
<%
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
%>
<a href="javascript:..."> 替代 onmouseover/onmouseout超連結 href 可直接執行 JS,配合 CSS 樣式,比寫滑鼠事件更簡潔:
<a href='javascript:phoneListJs.showTabbarContent("+serial_num+","+cmt+");' style='color:red;'>編輯</a>
工具方法:
public static String replaceIfMissing(String orig, String replacement) {
if (orig == null || orig.trim().isEmpty()) return replacement;
return orig;
}
ORDER BY NVL(nullable_col, 'ZZZ') 讓 NULL 排最後;NVL(col, ' ') 讓 NULL 排最前。