I am using Spring and Thymeleaf. In user registration form I am posting things to specific URL. In Spring I am checking if some errors occures. So if there are some errors I am adding some attributes used to show errors. Than I dont want Spring to redirect anywhere. I want him to update model and do nothing else. Is there any chance I can achieve that?
我正在使用Spring和Thymeleaf。在用户注册表单中,我将内容发布到特定URL。在Spring,我正在检查是否发生了一些错误。因此,如果存在一些错误,我会添加一些用于显示错误的属性。比我不希望Spring重定向任何地方。我希望他更新模型,不做任何其他事情。我有机会实现这一目标吗?
Controller
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute Person person, Model model){
model = loginPageService.register(person, model);
return "loginPage";
}
Service
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;
}
Some of html
一些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>
2 个解决方案
#1
0
Thymeleaf + Spring are creating HTML views on server side, so it need to redirect and create new HTML view with errors. You can create some API to checks if email exist and use AJAX to handle this.
Thymeleaf + Spring正在服务器端创建HTML视图,因此需要重定向并创建有错误的新HTML视图。您可以创建一些API来检查电子邮件是否存在并使用AJAX来处理此问题。
If your project is small and you don't care about efficiency and security you can pass all not available email to view and try to validate this on view side (but this solution is worst solution)
如果您的项目很小并且您不关心效率和安全性,您可以将所有不可用的电子邮件传递给查看并尝试在视图方面验证这一点(但此解决方案是最糟糕的解决方案)
#2
0
you'd better use Spring Validator
你最好使用Spring Validator
create a class Validator like below
创建一个类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
}
}
Later in your controller
稍后在您的控制器中
@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 are creating HTML views on server side, so it need to redirect and create new HTML view with errors. You can create some API to checks if email exist and use AJAX to handle this.
Thymeleaf + Spring正在服务器端创建HTML视图,因此需要重定向并创建有错误的新HTML视图。您可以创建一些API来检查电子邮件是否存在并使用AJAX来处理此问题。
If your project is small and you don't care about efficiency and security you can pass all not available email to view and try to validate this on view side (but this solution is worst solution)
如果您的项目很小并且您不关心效率和安全性,您可以将所有不可用的电子邮件传递给查看并尝试在视图方面验证这一点(但此解决方案是最糟糕的解决方案)
#2
0
you'd better use Spring Validator
你最好使用Spring Validator
create a class Validator like below
创建一个类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
}
}
Later in your controller
稍后在您的控制器中
@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";
}