在 Struts 應用中,表單提交驗證失敗返回原頁面時,如何保持多選框的勾選狀態?關鍵在於 ActionForm 中每個多選框對應獨立的屬性名稱,而非共用同一屬性。
若多個 `
<html:checkbox property="testBox1" value="chargeStat01">處罰開始</html:checkbox>
<html:checkbox property="testBox2" value="chargeStat01">處罰開始</html:checkbox>
public class ChargeStatForm extends ActionForm {
private String testBox1;
private String testBox2;
public String getTestBox1() { return testBox1; }
public void setTestBox1(String testBox1) { this.testBox1 = testBox1; }
public String getTestBox2() { return testBox2; }
public void setTestBox2(String testBox2) { this.testBox2 = testBox2; }
}
Struts 表單標籤根據 `property` 屬性對應 ActionForm 的 getter/setter。每個多選框擁有獨立屬性時,框架能正確將提交的參數綁定至對應欄位,重新渲染頁面時 `