我正在使用Spring和Thymeleaf。在用户注册表单中,我将内容发布到特定URL。在Spring,我正在检查是否发生了一些错误。因此,如果存在一些错误,我会添加一些用于显示错误的属性。比我不希望Spring在任何地方重定向。我希望他更新模型,不做任何其他事情。我有机会实现这个目标吗?
控制器
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute Person person, Model model){
model = loginPageService.register(person, model);
return "loginPage";
}
服务
public Model register(Person person, Model model){
//check if user exists
Person existingPerson = personService.findByEmail(person.getEmail());
if(existingPerson != null){
model.addAttribute("emailUsedError", true);
}
else{
personService.save(person);
model.addAttribute("registered", true);
}
return model;
}
部分HTML
<div id="singup">
<p class="error" th:if="${registerError}">Wypełnij wszystkie pola!</p>
<p class="error" th:if="${emailUsedError}">Podany email został już użyty!</p>
<form th:action="@{/register}" th:object="${person}" method="post" onsubmit="return checkPassword(this)">
<input type="text" id="imie" name="firstName" placeholder="Imię" onfocus="this.placeholder=''"
onblur="this.placeholder='Imię'" required>
<input type="text" id="nazwisko" name="lastName" placeholder="Nazwisko" onfocus="this.placeholder=''"
onblur="this.placeholder='Nazwisko'" required>
<input type="text" id="identityNumber" name="identityNumber" placeholder="Nr. indeksu" onfocus="this.placeholder=''"
onblur="this.placeholder='Nr. indeksu'" required>
<input type="email" id="email" name="email" placeholder="example@google.com"
onfocus="this.placeholder=''" onblur="this.placeholder='example@google.com'" required>
<input type="password" id="password" name="password" placeholder="Hasło" onfocus="this.placeholder=''"
onblur="this.placeholder='Hasło'" required>
<input type="password" id="confirm_password" name="confirm_password"
placeholder="Potwierdź hasło" onfocus="this.placeholder=''" onblur="this.placeholder='Potwierdź hasło'" required>
<input type="submit" value="Zarejestruj">
</form>
</div>
&#13;
答案 0 :(得分:0)
你最好使用Spring Validator
创建一个类Validator,如下所示
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.med.quraa.models.User;
@Component(value = "personValidator")
public class PersonValidator implements Validator{
@Override
public boolean supports(Class<?> arg0) {
return Person.class.isAssignableFrom(arg0);
}
@Override
public void validate(Object form, Errors errors) {
Person person=(Person) form;
Person existingPerson = personService.findByEmail(person.getEmail());
if(existingPerson != null){
errors.rejectValue("email","", "User already exists");}
if(person.getIdentityNumber.isEmpty()){
errors.rejectValue("identityNumber","", "Identifier cannot be blank");}
//validate Other Fields
}
}
稍后在你的控制器中
@Autowired
private PersonValidator validator;
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute @Valid Person person ,BindingResult result,final Model model) {
validator.validate(Person, result);
if(result.hasErrors()){
model.addAttribute("errorMsg",result.getAllErrors().get(0).getCode());
//you can manipulate all errors here
return "register";
}
loginPageService.register(person);//you dont need to add Model as param
return "loginPage";
}
答案 1 :(得分:0)
Thymeleaf + Spring正在服务器端创建HTML视图,因此需要重定向并创建有错误的新HTML视图。您可以创建一些API来检查电子邮件是否存在并使用AJAX来处理此问题。
如果你的项目很小并且你不关心效率和安全性,你可以传递所有不可用的电子邮件来查看并尝试在视图方面验证这一点(但这个解决方案是最糟糕的解决方案)