Struts2 <s:checkboxlist> 標籤設置 cssClass 屬性後,生成的 <input> 元素缺少 class 屬性。本文深度分析 FreeMarker 模板源碼,提供精確修復方案。
<!-- JSP 頁面 -->
<s:checkboxlist name="interests" list="interestList" cssClass="form-check-input" />
<!-- 生成的 HTML (缺少 class) -->
<input type="checkbox" name="interests" value="1" id="interests-1" />
<!-- 預期: class="form-check-input" -->
Struts2 默認模板路徑:template/simple/checkboxlist.ftl (或 template/xhtml/checkboxlist.ftl)。
原模板片段:
<#assign itemCount = 0/>
<#if parameters.list??>
<@s.iterator value="parameters.list">
<#assign itemCount = itemCount + 1/>
<#-- ... 獲取 itemKey, itemValue ... -->
<input type="checkbox"
name="${parameters.name?html}"
value="${itemKeyStr?html}"
id="${parameters.name?html}-${itemCount}"
<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)>
checked="checked"<#rt/>
</#if>
<#if parameters.disabled?default(false)>
disabled="disabled"<#rt/>
</#if>
<#if parameters.title??>
title="${parameters.title?html}"<#rt/>
</#if>
<!-- 缺失 cssClass 處理 -->
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
/>
</@s.iterator>
</#if>
原因:模板處理了 disabled、title 等屬性,但**漏掉了 cssClass (對應 class)** 的輸出邏輯。common-attributes.ftl 通常處理 id, name, tabindex 等通用屬性,不包含 class。
src/main/resources/template/simple/checkboxlist.ftl (或 template/xhtml/ 視主題而定)<input> 標籤中添加 cssClass 處理<#assign itemCount = 0/>
<#if parameters.list??>
<@s.iterator value="parameters.list">
<#assign itemCount = itemCount + 1/>
<#if parameters.listKey??>
<#assign itemKey = stack.findValue(parameters.listKey)/>
<#else>
<#assign itemKey = stack.findValue('top')/>
</#if>
<#if parameters.listValue??>
<#assign itemValue = stack.findString(parameters.listValue)?default("")/>
<#else>
<#assign itemValue = stack.findString('top')/>
</#if>
<#assign itemKeyStr = itemKey.toString() />
<input type="checkbox"
name="${parameters.name?html}"
value="${itemKeyStr?html}"
id="${parameters.name?html}-${itemCount}"
<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)>
checked="checked"<#rt/>
</#if>
<#if parameters.disabled?default(false)>
disabled="disabled"<#rt/>
</#if>
<#if parameters.title??>
title="${parameters.title?html}"<#rt/>
</#if>
<!-- ===== 修復:添加 cssClass 輸出 ===== -->
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
</#if>
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
/>
</@s.iterator>
</#if>
struts2-core-x.x.x.jarorg/apache/struts2/templates/simple/checkboxlist.ftl<!-- 生成的 HTML (修復後) -->
<input type="checkbox" name="interests" value="1"
id="interests-1" class="form-check-input" />
<input type="checkbox" name="interests" value="2"
id="interests-2" class="form-check-input" />
<!-- 後續項同樣包含 class -->
Struts2 <s:radio>, <s:checkbox> 等標籤可能存在類似問題,檢查對應模板:
template/simple/radio.ftltemplate/simple/checkbox.ftltemplate/simple/select.ftl統一添加 cssClass / cssStyle 處理邏輯:
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
</#if>
<#if parameters.cssStyle??>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
src/main/resources/template/ 目錄結構,僅覆蓋需修改的模板,保持升級友好theme="xhtml" 或 theme="css_xhtml",需對應覆蓋 template/xhtml/ 或 template/css_xhtml/ 下的模板新項目建議避免服務端標籤庫渲染 HTML,改用:
@Action + @Result(type="json"))