@InitBinder註解 解決form表單向springmvc傳遞多個物件引數
阿新 • • 發佈:2019-02-20
需求:有如下一個表單,包含兩個物件的屬性,name以“物件名.屬性名”命名,傳到後臺。
<form class="form-horizontal" role="form" action="updateCompanyResult.do" method="post">
<div class="alert alert-success" role="alert">
<strong>公司資訊 —— </strong>基本資訊
</div>
<div class="form-group">
<label for ="firstname" class="col-sm-2 control-label">公司中文全稱</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="請輸入公司中文全稱" value="${company.companyInfoBase.gszwqc}" name="companyInfoBase.gszwqc">
</div>
</div>
<div class="form-group" >
<label for="lastname" class="col-sm-2 control-label">英文名稱及縮寫</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="請輸入英文名稱及縮寫" value="${company.companyInfoBase.ywmcjsx}" name="companyInfoBase.ywmcjsx">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">證券簡稱</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.zqjc}" name="companyInfoBase.zqjc">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">證券程式碼</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.zqdm}" name="companyInfoBase.zqdm">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">法定代表人</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.fddlr}" name="companyInfoBase.fddlr">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">註冊地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.zcdz}" name="companyInfoBase.zcdz">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">辦公地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.bgdz}" name="companyInfoBase.bgdz">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">會計師事務所</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.kjssws}" name="companyInfoBase.kjssws">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">簽字註冊會計師姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.qzzckjsxm}" name="companyInfoBase.qzzckjsxm">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">會計師事務所辦公地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoBase.kjsswsbgdz}" name="companyInfoBase.kjsswsbgdz">
</div>
</div>
<div class="alert alert-success" role="alert">
<strong>公司資訊 —— </strong>聯絡方式
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">董事會祕書或資訊披露負責人</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.dshmshxxplfzr}" name="companyInfoContact.dshmshxxplfzr">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">電話</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.tel}" name="companyInfoContact.tel">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">傳真</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.fax}" name="companyInfoContact.fax">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">電子郵箱</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.email}" name="companyInfoContact.email">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">公司網址</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.url}" name="companyInfoContact.url">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">聯絡地址及郵政編碼</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.lxdzjyzbm}" name="companyInfoContact.lxdzjyzbm">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">公司指定資訊披露平臺的網址</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.gszdxxplptdwz}" name="companyInfoContact.gszdxxplptdwz">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">公司年度報告備置地</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${company.companyInfoContact.gsndbgbzd}" name="companyInfoContact.gsndbgbzd">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">儲存</button>
</div>
</div>
</form>
直接使用物件接收是接收不到值的。需要加入如下程式碼
// 繫結變數名字和屬性,引數封裝進類
@InitBinder("companyInfoBase")
public void initBinderUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix("companyInfoBase.");
}
// 繫結變數名字和屬性,引數封裝進類
@InitBinder("companyInfoContact")
public void initBinderAddr(WebDataBinder binder) {
binder.setFieldDefaultPrefix("companyInfoContact.");
}
controller方法體:
@RequestMapping("updateCompanyResult.do")
public ModelAndView updateCompanyResult(@ModelAttribute CompanyInfoBase companyInfoBase,@ModelAttribute CompanyInfoContact companyInfoContact,ModelAndView model){
model.setViewName("jsps/pdf/companyResult");
return model;
}