1 <html:select property="brandId">
2 <html:optionsCollection name="brands" value="id" label="name"/>
3 </html:select>
首先是添加時要初始化brands:
2 <html:optionsCollection name="brands" value="id" label="name"/>
3 </html:select>
1 @Override
2 protected void refrenceData(HttpServletRequest request) {
3 request.setAttribute("brands", modelManager.getBrand());
4 }
其次是修改時要在下拉菜單顯示已選值:2 protected void refrenceData(HttpServletRequest request) {
3 request.setAttribute("brands", modelManager.getBrand());
4 }
1 protected void onInitForm(ActionForm form, HttpServletRequest request,TModel model) {
2
3 if (model.getBrand() != null) {
4 LazyValidatorForm bookForm = (LazyValidatorForm) form;
5 bookForm.set("brandId",model.getBrand().getId());
6 }
7 }
問題:在添加時要注意不是model!=null,而是model的brand!=null才賦值
2
3 if (model.getBrand() != null) {
4 LazyValidatorForm bookForm = (LazyValidatorForm) form;
5 bookForm.set("brandId",model.getBrand().getId());
6 }
7 }