モデルに個別の電子メールエラーメッセージを手動で追加しようとしていますが、ビューに何も表示されません。
私はそれがBindingResultにObjectErrorを作成しているか、それをどのようにしているのかと思います。
キャッチ内にエラーを追加しています。私は空の電子メールのフィールドを残し、JSR-303のアノテーションは、(ビューにエラーが表示さ)で蹴るときここカスタムオブジェクトフィールド用のSpring 3 DataBinderにエラーメッセージを追加する
はresult.errorsの内容は次のとおりです。ここで
[Field error in object 'user' on field 'email': rejected value []; codes [NotEmpty.user.email,NotEmpty.email,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.email,email]; arguments []; default message [email]]; default message [may not be empty]]
は、結果の内容です。私は手動でErrorObjectを追加した後のエラー(メールエラーは、ビューには表示されません):
[Error in object 'email': codes []; arguments []; default message [An account already exists for this email.]]
コントローラ:
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView post(@Valid User user, BindingResult result)
{
if (result.hasErrors())
{
ModelAndView modelAndView = new ModelAndView(
Consts.MODEL_RESISTER_PAGE);
modelAndView.addObject("user", user);
return modelAndView;
}
else
{
try
{
userService.addUser(user);
ModelAndView modelAndView = new ModelAndView(
Consts.MODEL_CARD_REPORTS_HOME_PAGE);
modelAndView.addObject("userRegisteredSuccess", Boolean.TRUE);
return modelAndView;
}
catch (DataIntegrityViolationException ex)
{
ObjectError error = new ObjectError("email","An account already exists for this email.");
result.addError(error);
ModelAndView modelAndView = new ModelAndView(
Consts.MODEL_RESISTER_PAGE);
modelAndView.addAllObjects(result.getModel());
modelAndView.addObject("user", user);
return modelAndView;
}
}
}
マイモデル:
@Entity
public class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -5232533507244034448L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
@Size(min=2, max=15)
private String firstname;
@NotEmpty
@Size(min=2, max=15)
private String surname;
@NotEmpty
@Email
private String email;
@NotEmpty
@Size(min=6, max=10)
private String password;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getSurname()
{
return surname;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
add.html
<form id="registrationForm" action="#"
th:action="@{/registration}" th:object="${user}" method="post"
class="clearfix">
<legend>Registration</legend>
<div class="control-group input"
th:class="${#fields.hasErrors('firstname')}? 'control-group input error'">
<input type="text" th:field="*{firstname}"
placeholder="Firstname" /> <span class="help-block"
th:if="${#fields.hasErrors('firstname')}"
th:errors="*{firstname}"></span>
</div>
<div class="control-group input"
th:class="${#fields.hasErrors('surname')}? 'control-group input error'">
<input type="text" th:field="*{surname}" placeholder="Surname" />
<span class="help-block"
th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></span>
</div>
<div class="control-group input"
th:class="${#fields.hasErrors('email')}? 'control-group input error'">
<input type="text" th:field="*{email}" placeholder="Email" />
<span class="help-block" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></span>
</div>
<div class="control-group input"
th:class="${#fields.hasErrors('password')}? 'control-group input error'">
<input type="password" th:field="*{password}"
placeholder="Password" /> <span class="help-block"
th:if="${#fields.hasErrors('password')}"
th:errors="*{password}"></span>
</div>
<div class="clearfix">
<input type="submit" class="btn btn-success btn-large"
value="Register" />
</div>
</form>
優秀 - 私はrejectValueメソッドを認識していませんでした。それは問題のゼリーを修正しました。ありがとう –
**ローカライズされたメッセージ**を 'rejectValue'に入れたように、**ローカライズされたメッセージ**すなわち' error.user 'を 'ObjectError'に入れることができますか?私は 'ObjectError'を使用すると、ローカライズされたメッセージを取得できません。 'rejectValue'を使って、ローカライズされたメッセージを取得しています。あなたは助けてもらえますか? –
なぜあなたは 'ObjectError'を使用していますか?あなたのコントローラにエラーを追加するには、 'ObjectError'インスタンスの代わりに' rejectValue'メソッドを使うべきです。こうすることで、ローカライズされたメッセージが表示されます。 – jelies