Form input constraints not being enforced? - spring

I'm new to Tomcat and Spring Web. I'm trying to use Spring's form validation features by following this tutorial. Everything seems to run smoothly except for one thing... my form doesn't do any validation and I can always get to the success page when I send the form no matter which data I provide.
Am I using the constraints correctly? I want to enforce that the user fills in their first name and that the first name be at least two characters long.
package net.devmanuals.form;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class RegistrationForm {
#NotEmpty(message = "You surely have a name, don't you?")
#Size(min = 2, message = "I'm pretty sure that your name consists of more than one letter.")
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
}
Form code:
<form:form method="post" commandName="regform">
<p><form:input path="firstName" /> <form:errors path="firstName" /></p>
<p><input type="submit" /></p>
</form:form>
The controller:
#Controller
#RequestMapping("/register")
public class RegistrationController {
#RequestMapping(method = RequestMethod.GET)
public String showRegForm(Map model) {
RegistrationForm regForm = new RegistrationForm();
model.put("regform", regForm);
return "regform";
}
#RequestMapping(method = RequestMethod.POST)
public String validateForm(#Valid RegistrationForm regForm, BindingResult result, Map model) {
if (result.hasErrors()) {
return "regform";
}
model.put("regform", regForm);
return "regsuccess";
}
}
Am I applying the constraints incorrectly?

In addition to adding <mvc:annotation-driven/> to your config, you need to make sure the JSR-303 jar is on your classpath. From the docs:
[AnnotationDrivenBeanDefinitionParser] ... configures the validator if specified, otherwise defaults to a fresh Validator instance created by the default LocalValidatorFactoryBean if the JSR-303 API is present on the classpath.

Related

Micronaut - What is the #ModelAttribute equivalent

In Spring I know I can bind thymeleaf form inputs to a command object using #ModelAttribute in the controller, but is there an equivalent in Micronaut? I can't see it documented anywhere
Mapping form data in Micronaut is straight forward.
Given a form containing two input fields
<input name="name"/>
<input name="location"/>
you can handle them on the backend either
Binding them to parameters
#Controller("/form")
public class FormController {
#Post
public HttpResponse processForm(String name, String location) {
return HttpResponse.created();
}
}
or
Bind form data to POJO
public class MyForm {
private String name;
private String location;
// getter/setter omitted
}
#Controller("/form")
public class FormController {
#Post
public HttpResponse processForm(#Body MyForm) {
return HttpResponse.created();
}
}

Spring Boot Freemarker status.errorMessages not populated

I am trying to get model validation working with my spring boot mvc application. I am using freemarker as view templates. My problem is now that even though model validation works as expected the model errors are not shown in my view.
Here is some example code. The Model:
public class TestModel {
#Size(min=2, max=10)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The Controller
#Controller
#RequestMapping("/test")
public class OrderController extends BaseController {
#RequestMapping(value = "new/greenhouse", method = RequestMethod.GET)
public String get(#ModelAttribute TestModel testModel) {
testModel.setName("Hello world");
return "test.ftl";
}
#RequestMapping(value = "/test", method = RequestMethod.POST)
public String post(#ModelAttribute #Valid TestModel testModel, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
// The bindingResult contains the "correct" errors
return "test.ftl";
}
return "redirect:/";
}
}
The view:
<#import "../layout.ftl" as layout />
<#import "/spring.ftl" as spring />
<#import "../freemarker.ftl" as ftl />
<#layout.defaultLayout>
<form action="/test" method="post">
<#ftl.csrfToken />
Name:
<#spring.formInput "testModel.Name" />
<#spring.showErrors "<br>" /> <#-- Does not print the error as status.errorMessages is not populated -->
<#-- This will at least display the default Message -->
<#list spring.status.errors.allErrors as error>
<span class="has-error">${error.defaultMessage} </span>
</#list>
</form>
</#layout.defaultLayout>
Any idea why status.errorMessages does not get populated?
So I finally found the solution to this:
The property name in the Freemarker view has to be lowercase (as is the private member name in the model class). The correct view would look like this:
<#spring.formInput "testModel.name" />
<#spring.showErrors "<br>" />

Spring - Model attributes after BindingResult with errors

In the following piece of code I just want to create a new user and link it to the selected groups.
Everything works fine when the user and group are valid. The problem comes when the bindingresult has errors. The controller detects such error (all fine so far) and returns the same view (I want to keep the data entered by the user) but the list of groups is empty (I have discovered that, after showing again the view, userform.groups is null).
Has anyone a clue about what the problem could be?
UserForm
#Component
public class UserForm {
#Valid
private User user;
#Valid
private Collection<Group> allGroups;
// Setters and getters
}
UserController
#Controller
public class UserController {
#Autowired
UserGroupService userGroupService;
#Autowired
BCryptPasswordEncoder passwordEncoder;
#InitBinder
public void initBinder (WebDataBinder binder) {
binder.registerCustomEditor(Set.class, "userform.user.groups", new GroupListEditor(userGroupService));
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.GET)
public ModelAndView createUsetGet () {
ModelAndView mav = new ModelAndView("/admin/users/CreateUser");
UserForm userForm = new UserForm();
userForm.setUser(new User());
userForm.setGroups(userGroupService.getAllEnabledGroups());
mav.addObject("userform", userForm);
return mav;
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.POST)
public String createUserPost (#Valid #ModelAttribute("userform") UserForm userForm, BindingResult result) {
if (result.hasErrors() == true) {
return "/admin/users/CreateUser";
}
userForm.getUser().setPassword(passwordEncoder.encode(userForm.getUser().getPassword()));
userGroupService.saveUser(userForm.getUser());
return "redirect:/admin/users/ViewUsers";
}
}
CreateUser.jsp (Only piece regarding the groups)
<form:form modelAttribute="userform" method="post">
Username:
<form:input path="user.loginName"/>
<!-- More fields -->
<form:select path="user.groups" multiple="true">
<form:options items="${userform.groups}" itemValue="id" itemLabel="name" />
</form:select>
<button type="submit">Create</button>
</form:form>
Any help is appreciated!
The object gets recreated and values are bound to the resulting object. Which means no group objects.
Also those shouldn't be in the object at all. To solve use a #ModelAttribute annotated method, which will be invoked for each request handling method and create an object and fill the list of groups.
#ModelAttribute
public void init(Model model) {
UserForm userForm = new UserForm();
userForm.setUser(new User());
model.addAttribute("userform", userForm);
model.addAtrribute("groups", userGroupService.getAllEnabledGroups());
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.GET)
public String createUsetGet () {
return "/admin/users/CreateUser";
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.POST)
public String createUserPost (#Valid #ModelAttribute("userform") UserForm userForm, BindingResult result) {
if (result.hasErrors() == true) {
return "/admin/users/CreateUser";
}
userForm.getUser().setPassword(passwordEncoder.encode(userForm.getUser().getPassword()));
userGroupService.saveUser(userForm.getUser());
return "redirect:/admin/users/ViewUsers";
}
Ofcourse your jsp has to change slightly also.
<form:select path="user.groups" multiple="true">
<form:options items="${groups}" itemValue="id" itemLabel="name" />
</form:select>
There is one drawback of using this approach now the userGroupService.getAllEnabledGroups() is called for each incoming request. This might not be needed. You could store those in the session using the #SessionAttributes annotation on the class.
#Controller
#SessionAttributes("groups")
public class UserController {
#Autowired
UserGroupService userGroupService;
#Autowired
BCryptPasswordEncoder passwordEncoder;
#InitBinder
public void initBinder (WebDataBinder binder) {
binder.registerCustomEditor(Set.class, "userform.user.groups", new GroupListEditor(userGroupService));
}
#ModelAttribute("groups")
public List<Group> groups() {
return userGroupService.getAllEnabledGroups();
}
#ModelAttribute("userform")
public UserForm userform() {
UserForm userForm = new UserForm();
userForm.setUser(new User());
return userForm;
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.GET)
public String createUsetGet () {
return "/admin/users/CreateUser";
}
#RequestMapping(value = "/admin/users/CreateUser", method = RequestMethod.POST)
public String createUserPost (#Valid #ModelAttribute("userform") UserForm userForm, BindingResult result, SessionStatus status) {
if (result.hasErrors() == true) {
return "/admin/users/CreateUser";
}
userForm.getUser().setPassword(passwordEncoder.encode(userForm.getUser().getPassword()));
userGroupService.saveUser(userForm.getUser());
status.setComplete();
return "redirect:/admin/users/ViewUsers";
}
}
You will then need, on success, to tell the SessionStatus that you are finished. If you don't do this your session might pollute.
It's because the information about the validation errors is lost after redirect.
You can solve this using RedirectAttributes. Check this tutorial.

Spring 3 #ModelAttribute with List in model

I have two models :
public class Size {
private String name;
// getter and setter
}
public class Product {
private int id;
private String designation;
private Float price;
private List<Size> availableSizes;
// Getter and setters
}
I have defined Sizes in my servlet, and now I nead to create products with the availables sizes.
What I do in my index Controller:
ModelAndView render = new ModelAndView("admin/index");
render.addObject("products", productFactory.getProducts());
render.addObject("sizes", sizeFactory.getSizes());
render.addObject("command", p);
return render;
I've a list of products, and my list of sizes.
In my index view, I do:
<form:form method="post" action="/ecommerce/admin/products" class="form-horizontal">
<form:input path="id" />
<form:input path="designation" />
<form:input path="price" />
<form:select path="availableSizes" items="${sizes}"/>
<input type="submit" value="Ajouter le produit" class="btn" />
</form:form>
Then, in new product controller, I do :
// To fix: Sizes not saved !
#RequestMapping(value = "/products", method = RequestMethod.POST)
public ModelAndView newProduct(#ModelAttribute("Product") Product product,
BindingResult result) {
productFactory.add(product);
return buildIndexRender(null, null, product);
}
The problem is that I keet the posted product, but not the corresponding sizes.
Is there a way to keep selected sizes in the form int the controler, or directly in the model?
Thanks.
It's a very common problem.
To set a List<Size> in Product instance from the post data, that is a string colon separated list with selected sizes, all you need to do is tell the framework how to convert a size String to a Size instance. The most common approach is registering a PropertyEditor on WebDataBinder
class SizePropertyEditor extends PropertyEditorSupport {
#Override
public void setAsText(String text) throws IllegalArgumentException {
Size size = stringToSize(text); // write this code
setValue(size);
}
}
and register the property editor in Controller using #InitBinder annotation
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Size.class, new SizePropertyEditor());
}
For a more generic approach see ConversionService.

Spring Framework - New object is created in DB instead of updating

I have an application written in Spring 3.0 hooked up using Hibernate to a database. I have a controller to an update form. Whenever the form is submitted, I expect the object that is shown to be updated however a new object is created with a new ID value. I've looked over the "petclinic" sample and i can't see how it is different.
POJO
public class Person
{
private int id;
#NotNull
private String name;
//getter/setter for id
//getter/setter for name
}
Controller
public class PersonUpdateController
{
//injected
private PersonService personService;
#RequestMapping(value="/person/{personId}/form", method=RequestMethod.POST)
public String updateForm(ModelMap modelMap, #PathVariable personId)
{
Person person = personService.getById(personId);
modelMap.addAttribute(person);
return "person/update";
}
#RequestMapping(value="/person/{personId}", method=RequestMethod.POST)
public String update(ModelMap modelMap, #Valid Person person, BindingResult bindingResult)
{
if(bindingResult.hasErrors())
{
modelMap.addAttribute(person);
return "person/update";
}
personService.save(person);
return "redirect:person/" + person.getId() + "/success";
}
}
JSP
<spring:url value="/person/${person.id}" var="action_url" />
<spring:form action="${action_url}" modelAttribute="person" method="POST">
<spring:input name="name" path="name" />
<input type="submit" value="Save" />
</spring:form>
PersonService Implementation
public class HibernatePersonService
implements PersonService
{
//injected
private SessionFactory sessionFactory;
//other methods
public void save(Person person)
{
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(person);
}
}
Spring MVC doesn't do any magic with HTML forms. Since your form contains only one field, you get only one field populated in update method. So, you have two options:
Pass id as a hidden field in the form: <spring:hidden path = "id" />. Note that in this case you need to check possible consequences for security (what happens if malicious person changes the id).
Store Person in the session so that data from the form is used to update the stored object (note that it may cause interference if several instances of the form is opened in one session). That's how it's done in Petclinic:
-
#SessionAttributes("person")
public class PersonUpdateController {
...
#RequestMapping(value="/person/{personId}", method=RequestMethod.POST)
public String update(ModelMap modelMap, #Valid Person person,
BindingResult bindingResult, SessionStatus status)
{
...
personService.save(person);
status.setComplete(); // Removes person from the session after successful submit
...
}
#InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); // For security
}
}

Resources