數據綁定:自動將 HTTP 請求參數(String)轉換綁定到控制器方法參數或命令對象屬性,支持類型轉換、格式化、驗證。失敗時自動回顯表單數據,提升用戶體驗。
<%@ taglib prefix="form" uri="https://www.springframework.org/tags/form" %>
| 標簽 | 渲染 HTML | 核心屬性 | 典型場景 |
|---|---|---|---|
<form:input> | <input type="text"> | path, id, cssClass | 單行文本輸入 |
<form:password> | <input type="password"> | path, showPassword | 密碼輸入 |
<form:textarea> | <textarea> | path, rows, cols | 多行文本 |
<form:checkbox> | <input type="checkbox"> | path, value, label | 單選/布爾值 |
<form:checkboxes> | 多個 checkbox | path, items, itemValue, itemLabel | 多選綁定集合 |
<form:radiobutton> | <input type="radio"> | path, value, label | 單選組 |
<form:radiobuttons> | 多個 radio | path, items, itemValue, itemLabel | 單選綁定集合 |
<form:select> | <select> | path, items, itemValue, itemLabel | 下拉選單 |
<form:option> | <option> | value, label | 手動添加選項 |
<form:options> | 多個 option | items, itemValue, itemLabel | 批量綁定選項 |
<form:errors> | <span> | path, cssClass | 顯示驗證錯誤 |
public class User {
private String username;
private String password;
private Boolean married;
private List<String> hobbies;
private String career;
private String remark;
// getter/setter 省略
}
@Controller
public class UserController {
@GetMapping("/user/input")
public String showForm(Model model) {
Map<Integer, String> hobbyMap = Map.of(
1, "籃球", 2, "羽毛球", 3, "游泳", 4, "閱讀"
);
model.addAttribute("user", new User());
model.addAttribute("hobbyMap", hobbyMap);
model.addAttribute("careers", new String[]{"教師", "工程師", "醫生", "其他"});
return "userForm";
}
@PostMapping("/user/save")
public String save(@ModelAttribute User user, BindingResult result) {
if (result.hasErrors()) return "userForm";
userService.save(user);
return "redirect:/user/list";
}
}
<form:form modelAttribute="user" method="post" action="save">
<div>
<label>用戶名:</label>
<form:input path="username" />
<form:errors path="username" cssClass="error" />
</div>
<div>
<label>密碼:</label>
<form:password path="password" />
</div>
<div>
<label>婚姻狀況:</label>
<form:checkbox path="married" value="true" label="已婚" />
</div>
<div>
<label>興趣愛好:</label>
<form:checkboxes items="${hobbyMap}" path="hobbies" itemKey="key" itemValue="value" />
</div>
<div>
<label>職業:</label>
<form:select path="career">
<form:option value="" label="--請選擇--" />
<form:options items="${careers}" />
</form:select>
</div>
<div>
<label>備註:</label>
<form:textarea path="remark" rows="4" />
</div>
<button type="submit">提交</button>
</form:form>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param>
<init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param>
</filter>
Spring 內置支持基本類型、日期、集合等轉換。自定義轉換器實現 Converter<S, T> 並註冊到 FormattingConversionService。
hobbies = new ArrayList<>()_propertyName 為 off,無需手動處理@DateTimeFormat(pattern="yyyy-MM-dd") 或全局註冊 FormatterSpring MVC 表單標簽庫通過 path 屬性實現雙向綁定,極大簡化了表單開發。結合 Bean Validation (JSR-380) 與 <form:errors>,可構建健壯的服務端驗證體系。掌握 checkboxes/select 的 items/itemKey/itemValue/itemLabel 組合用法,是處理複雜表單的關鍵。