« ①Tera5 Validatior の検証、 | ホーム | ③InitBinder を複数、Contoroller別に適用したテスト »
2017年9月 3日
②InitBinder を複数準備し @RequestMapping別に使うSpringMVC技術の検証
SpringValidator を二箇所指定 ↓
---
public class ManageCustomerController {
@Inject
CustomerService customerService;
@Inject
CustomerPassEqualsValidator passwordEqualsValidator;
@Inject
CustomerNameValidatorTESTforContA nameValidator;
@Inject
CustomerNameValidatorTESTforContB nameValidator2;
@Inject
CustomerBirthdayValidator dateValidator;
@Inject
Mapper beanMapper;
@Value("${customer.initialBirthYear}")
/// (メモ)これで -web main/resources/META-INF/spring/application.properties の値をとって来る。
Integer initialBirthYear;
@Value("${customer.initialBirthMonth}")
Integer initialBirthMonth;
@Value("${customer.initialBirthDay}")
Integer initialBirthDay;
/////////////////////////////////////// 以下のようにinitBinderを二種類指定 ///////////////
@InitBinder("customerForm")
public void initBinder(WebDataBinder webDataBinder) {
// add custom validators to default bean validations
// webDataBinder.addValidators(passwordEqualsValidator, dateValidator);
webDataBinder.addValidators(passwordEqualsValidator, dateValidator, nameValidator);
}
@InitBinder("customerFormTEST")
public void initBinderTEST(WebDataBinder webDataBinder) {
// add custom validators to default bean validations
// webDataBinder.addValidators(passwordEqualsValidator, dateValidator);
webDataBinder.addValidators(nameValidator2);
}
///// TEST Spring Validator A /////////////////////////////////////////////////////////////
@Component
public class CustomerNameValidatorTESTforContA implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return (CustomerForm.class).isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
CustomerForm customer = (CustomerForm) target;
String name = customer.getCustomerName();
///
if ( name.length() >= 3 ) {
return;
}
if ( name.length() < 3) {
errors.rejectValue("customerName", "IncorrectName.inputName",
"Name is larger than 3.(InitValidatorTEST T.IKEDA)");
}
}
}
///// TEST Spring Validator B ///////////////
@Component
public class CustomerNameValidatorTESTforContB implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return (CustomerFormTEST.class).isAssignableFrom(clazz); //////// ← 別Form
}
@Override
public void validate(Object target, Errors errors) {
CustomerFormTEST customer = (CustomerFormTEST) target; //////// ← 別Form
String name = customer.getCustomerName();
if ( name.length() >= 3 ) {
return;
}
if ( name.length() < 3) {
errors.rejectValue("customerName", "IncorrectName.inputName", /// application-messges_en.properties
"Name is larger than 3.(InitValidatorTEST T.IKEDA)");
}
// 追加
if ( Pattern.matches("^[^ -~。-゚]+$", name) ){
errors.rejectValue("customerName", "Pattern.customerForm.customerName",
"氏名は全角で入力してください。(InitValidatorTEST T.IKEDA)");
}
}
////// Controller 試験 /////////////////////////////////////////////////////////////
/**
* pre-initialization of form backed bean
* @return
*/
@ModelAttribute
public CustomerForm setUpCustomerForm() {
CustomerForm form = new CustomerForm();
form.setCustomerBirthYear(initialBirthYear);
form.setCustomerBirthMonth(initialBirthMonth);
form.setCustomerBirthDay(initialBirthDay);
log.debug("populate form {}", form);
return form;
}
@ModelAttribute
public CustomerFormTEST setUpCustomerFormTEST() {
CustomerFormTEST form = new CustomerFormTEST();
form.setCustomerBirthYear(initialBirthYear);
form.setCustomerBirthMonth(initialBirthMonth);
form.setCustomerBirthDay(initialBirthDay);
log.debug("populate form {}", form);
return form;
}
/**
* pre-initialization of form backed bean
* @return
*/
@RequestMapping(value = "create", method = RequestMethod.GET, params = "form")
public String createForm() {
return "managecustomer/createForm";
}
/**
* Return to main input screen
* @return
*/
@RequestMapping(value = "create", method = RequestMethod.POST, params = "redo")
public String createRedo(CustomerForm form) {
return "managecustomer/createForm";
}
@RequestMapping(value = "create", method = RequestMethod.POST, params = "redo2")
public String createRedoTEST(CustomerFormTEST form) {
return "managecustomer/createForm";
}
/**
* shows the confirmation screen before registering a new customer
* @param form
* @param result
* @return
*/
@TransactionTokenCheck(value = "create", type = TransactionTokenType.BEGIN)
@RequestMapping(value = "create", method = RequestMethod.POST, params = "confirm")
public String createConfirm(@Validated CustomerForm form,
BindingResult result) {
if (result.hasErrors()) {
return createRedo(form);
}
return "managecustomer/createConfirm";
}
/////////// TEST Controller /////////////
@TransactionTokenCheck(value = "create", type = TransactionTokenType.BEGIN)
@RequestMapping(value = "create", method = RequestMethod.POST, params = "tomo")
public String createConfirmTEST(@Validated CustomerFormTEST formTEST,
BindingResult result) {
if (result.hasErrors()) {
// CustomerForm form = beanMapper.map(formTEST, CustomerForm.class);
// return createRedo(form);
return createRedoTEST(formTEST);
}
return "managecustomer/createConfirm";
}
トラックバック(0)
トラックバックURL: http://erikay.cho88.com/cms/mt-tb.cgi/40
コメントする