比較的簡単な「パスワードの変更」ページで注釈を使用するようにSpringコントローラを更新しようとしています。このページの唯一のフィールドは「パスワード」と「パスワードの確認」です。フォームが送信されると、実際のパスワードの変更を行うWebサービスが呼び出されます。そのWebサービスは、そのサービス内で実行されるパスワードルールに基づいてInvalidPasswordExceptionを返すことがあります。だから、私は例外をキャッチし、エラーメッセージをビューに 'パスワード'フィールドの横に表示する追加します。速度コードはすでに#springShowErrorsを使って書かれていますので、そのタグで読み取れるようにエラーを追加します。Springの注釈付きコントローラのエラー検証を手動で処理します
は、ここに私のコントローラです:
@Controller
@RequestMapping("/edit-password.ep")
public class EditPasswordFormControllerImpl {
@Autowired
private CustomerService customerService;
@Autowired
private CustomerSessionService customerSessionService;
@RequestMapping(method = RequestMethod.POST)
protected ModelAndView onSubmit(@ModelAttribute("editPasswordFormBean") EditPasswordFormBeanImpl editPasswordFormBean, BindingResult errors, HttpServletRequest request) throws EpWebException {
String nextView = "redirect:/manage-account.ep";
final CustomerSession customerSession = (CustomerSession) request.getSession().getAttribute(WebConstants.CUSTOMER_SESSION);
final Customer customer = customerSession.getShopper().getCustomer();
try {
CustomerInfo customerInfo = new CustomerInfo();
customerInfo.setCustomerId(customer.getUserId());
customerInfo.setPassword(editPasswordFormBean.getPassword());
UpdateAccountServiceRequest updateRequest = new UpdateAccountServiceRequest();
updateRequest.setClientId(CLIENT_ID);
updateRequest.setCustomerInfo(customerInfo);
//this is the webservice call that could throw InvalidPasswordException
customerService.updateUserAccount(updateRequest);
} catch (InvalidPasswordException e) {
// This is where I'm not sure what to do.
errors.addError(new ObjectError("password", e.getMessage()));
nextView = "edit-password.ep";
} catch (ServiceException e) {
throw new EpWebException("Caught an exception while calling webservice for updating user", e);
}
return new ModelAndView(nextView);
}
@RequestMapping(method = RequestMethod.GET)
protected String setupForm(ModelMap model) {
EditPasswordFormBean editPasswordFormBean = new EditPasswordFormBeanImpl();
model.addAttribute("editPasswordFormBean", editPasswordFormBean);
return "account/edit-password";
}
}
そして、ここに私の速度のテンプレートの抜粋です:
<fieldset>
<legend>#springMessage("editPassword.editPasswordTitle")</legend>
<table border="0" cellspacing="0" cellpadding="3">
<colgroup>
<col width="150">
<col width="*">
</colgroup>
<tr>
<td colspan="2">
<br />
<strong>#springMessage("editPassword.changePassword")</strong>
</td>
</tr>
<tr>
<td align="right">#springMessage("editPassword.password")</td>
<td>
#springFormPasswordInput("editPasswordFormBean.password" "maxlength='100'")
#springShowErrors("<br>" "req")
</td>
</tr>
<tr>
<td align="right">#springMessage("editPassword.confirmPassword")</td>
<td>
#springFormPasswordInput("editPasswordFormBean.confirmPassword" "maxlength='100'")
#springShowErrors("<br>" "req")
</td>
</tr>
</table>
</fieldset>
私は例外をキャッチしたときに、私は何をすべきかはかなりよく分かりません。私が現在持っているものは動作しません。パスワードの編集ページに戻りますが、エラーは表示されません。私はHandleExceptionResolverについて読んだことがありますが、私がそれを使用しても、私はまだビューに表示するエラーを取得する方法がわかりません。
すべてのヘルプは大歓迎です!
私はそれを理解しました。それは実際に私がエラーを追加していた方法でした。 'errors.addError()'を使う代わりに、 'errors.rejectValue(" password "、InvalidPasswordException.class.getName()、e.getMessage());'を使うように変更しました。それはトリックを行うように見えた。しかし、提案のおかげで。私はそれについて考えなかった。私はデバッグモードで走っていましたが、例外がキャッチされたときでさえ、 'setupForm()'メソッドを打つことはありませんでした。 –
Cool !!あなたがこれを修正するために採用した技術を知っておいてよかったです。ありがとう – raddykrish