Reputation: 10761
Think this is an easy one but don't know how to handle it.
I have a form like this.
<c:url value="edit" var="editprofileUrl" />
<form:form class="form" id="signup" action="${editprofileUrl}" method="post" modelAttribute="editProfileForm">
<div class="formInfo">
<s:bind path="*">
<c:choose>
<c:when test="${status.error}">
<div class="text-danger">Unable to change profile. Please fix the errors below and resubmit.</div>
</c:when>
</c:choose>
</s:bind>
</div>
<div class="form-group">
<label for="firstName">First name</label>
<form:errors path="firstName" cssClass="text-danger" />
<form:input class="form-control" path="firstName" />
</div>
<div class="form-group">
<label for="lastName">Last name</label>
<form:errors path="lastName" cssClass="text-danger" />
<form:input class="form-control" id="last-name" path="lastName" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Save profile</button>
</div>
</form:form>
And the java form like this:
public class EditProfileForm {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
And a controller like this:
@Controller
@Transactional
public class EditProfileController {
@PersistenceContext
private EntityManager entityManager;
@RequestMapping(value = "/users/{username}/edit", method = RequestMethod.POST)
public String editProfile(@PathVariable String username, Principal currentUser, @Valid EditProfileForm form, BindingResult formBinding) {
if (formBinding.hasErrors()) {
return null;
}
Account user = entityManager.find(Account.class, currentUser.getName());
user.setFirstName(form.getFirstName());
user.setLastName(form.getLastName());
entityManager.persist(user);
return "home";
}
@RequestMapping(value = "/users/{username}/edit", method = RequestMethod.GET)
public ModelAndView editProfileForm(@PathVariable String username, Principal currentUser, WebRequest request, Model model) {
Account account = entityManager.find(Account.class, username);
if (account != null) {
model.addAttribute("account", account);
}
EditProfileForm form = new EditProfileForm();
form.setFirstName(account.getFirstName());
form.setLastName(account.getLastName());
return new ModelAndView("editprofile", "editProfileForm", form);
}
}
Everything works good besides when I don't fill in anything in one of the fields and it should give an error.
Something breaks in this code snippet:
if (formBinding.hasErrors()) {
return null;
}
Instead of returning the same page again with the errors it looks for a view that does not exist:
HTTP Status 404 - /project/WEB-INF/views/users/nilsi/edit.jsp
How do I return the same view again with the errors? Usually it works when i have a shorter @RequestMapping
like /signup
.
Thanks for any help on this!
Upvotes: 0
Views: 148
Reputation: 2900
In the event of binding errors, return the name of the form view again so that the user can correct the errors.
if (formBinding.hasErrors()) {
return "editprofile";
}
If you return null, Spring tries to guess a view name by looking at the url. Since your url ends with ".../edit", it tries to load edit.jsp.
Upvotes: 1