Java 使用SpringBoot中的Validate.properties文件验证表单
我试图在Spring Boot中验证表单,当我单击submit按钮时,jsp将向我显示如何填充字段(或不填充字段)的消息。我创建了validation.properties,其中包含消息,但我总是收到错误Java 使用SpringBoot中的Validate.properties文件验证表单,java,jsp,spring-boot,Java,Jsp,Spring Boot,我试图在Spring Boot中验证表单,当我单击submit按钮时,jsp将向我显示如何填充字段(或不填充字段)的消息。我创建了validation.properties,其中包含消息,但我总是收到错误“在区域设置“en_US”的代码下找不到消息”有人知道我如何在jsp中显示这些消息吗?我的validation.properties文件位于src/main/resources/static文件夹中,我在jsp文件中放置了validation.properties文件的链接。 在验证类中,我有:
“在区域设置“en_US”的代码下找不到消息”
有人知道我如何在jsp中显示这些消息吗?我的validation.properties文件位于
src/main/resources/static
文件夹中,我在jsp文件中放置了validation.properties文件的链接
。
在验证类中,我有:
public void validate(Object target, Errors errors) {
UserRegistration userRegistrated = (UserRegistration) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "ime", "NotEmpty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "prezime", "NotEmpty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "NotEmpty");
if (service.findUsers(userRegistrated.getEmail())) {
errors.rejectValue("email", "ExistUser");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
if (userRegistrated.getPassword().length() < 4) {
errors.rejectValue("password", "LengthPass");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "repeatPassword", "NotEmpty");
if (!userRegistrated.getRepeatPassword().equals(userRegistrated.getPassword())) {
errors.rejectValue("repeatPassword", "MatchPass");
}
}
我的jsp表单如下所示:
<form:form id="register" action="${action}" method="post" modelAttribute="userRegistration">
<table>
<tr>
<td><form:label path="ime">Ime:</form:label></td>
<td><form:input path="ime" type="text" placeholder="Ime" /></td>
<td><form:errors path="ime" /></td>
</tr>
<tr>
<td><form:label path="prezime">Prezime:</form:label></td>
<td><form:input path="prezime" type="text" placeholder="Prezime"/></td>
<td><form:errors path="prezime" /></td>
</tr>
<tr>
<td><form:label path="email">Korisnicko ime(e-mail):</form:label></td>
<td><form:input path="email" type="text" placeholder="Email" /></td>
<td><form:errors path="email" /></td>
</tr>
<tr>
<td><form:label path="password">Lozinka:</form:label></td>
<td><form:input path="password" type="password" placeholder="Lozinka"/></td>
<td><form:errors path="password" /></td>
</tr>
<tr>
<td><form:label path="repeatPassword">Potvrda lozinke:</form:label></td>
<td><form:input path="repeatPassword" type="password" placeholder="Potvrda lozinke"/></td>
<td><form:errors path="repeatPassword" /></td>
</tr>
<tr><td colspan="3"><button type="submit">Submit</button></td></tr>
</table>
</form:form>
我尝试将validation.properties放入
src/main/java
和src/main/resources
文件夹中,但得到了相同的结果。我认为问题在于如何访问validation.properties文件,但是我不知道怎么做?我需要为它写一些配置吗?我在互联网上搜索了一下,大部分都是关于spring(不是spring boot)的解释,我不知道是否是一样的..提前谢谢 解决了它,我只是在application.properties中添加了spring.messages.basename=validation
,并将validation.properties移动到src/main/resources。解决了它,我只是在application.properties中添加了spring.messages.basename=validation
,并将validation.properties移动到src/main/resources。
<form:form id="register" action="${action}" method="post" modelAttribute="userRegistration">
<table>
<tr>
<td><form:label path="ime">Ime:</form:label></td>
<td><form:input path="ime" type="text" placeholder="Ime" /></td>
<td><form:errors path="ime" /></td>
</tr>
<tr>
<td><form:label path="prezime">Prezime:</form:label></td>
<td><form:input path="prezime" type="text" placeholder="Prezime"/></td>
<td><form:errors path="prezime" /></td>
</tr>
<tr>
<td><form:label path="email">Korisnicko ime(e-mail):</form:label></td>
<td><form:input path="email" type="text" placeholder="Email" /></td>
<td><form:errors path="email" /></td>
</tr>
<tr>
<td><form:label path="password">Lozinka:</form:label></td>
<td><form:input path="password" type="password" placeholder="Lozinka"/></td>
<td><form:errors path="password" /></td>
</tr>
<tr>
<td><form:label path="repeatPassword">Potvrda lozinke:</form:label></td>
<td><form:input path="repeatPassword" type="password" placeholder="Potvrda lozinke"/></td>
<td><form:errors path="repeatPassword" /></td>
</tr>
<tr><td colspan="3"><button type="submit">Submit</button></td></tr>
</table>
</form:form>
NotEmpty.UserRegistration.ime=Polje je obavezno!
NotEmpty.UserRegistration.prezime=Polje je obavezno!
NotEmpty.UserRegistration.email=Polje je obavezno!
NotEmpty.UserRegistration.password=Polje je obavezno!
NotEmpty.UserRegistration.repeatPassword=Polje je obavezno!
ExistUser.UserRegistration.email=Korisnik sa takvim e-mailom vec postoji u bazi!
LengthPass.UserRegistration.password=Lozinka treba da je duza od 4 karaktera!
MatchPass.UserRegistration.repeatPassword=Lozinke se ne poklapaju!