Thursday, May 23, 2013

Spring MVC Portlet Validations

In the previous post we have implemented spring mvc portlet using Liferay Portal. In the following post we are going to implement input validation for adding new student form. We will use Spring’s Validator framework for validating data entered during adding new student.

1. First of all we will create StudentValidator class which implements Spring’s Validator interface.


[code language="java"]
@Component("studentValidator")
public class StudentValidator implements Validator{

@Override
public boolean supports(Class clazz) {
return Student.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "subject", "required.subject", "Field subject is required.");
}
}
[/code]

2. Now we will make changes in StudentController class

a. The @Autowired annotation is used to instruct the Spring container to inject the dependency. We will use this to inject StudentValidator in our StudentController class:

[code language="java"]
@Autowired
StudentValidator studentValidator;
[/code]

b. Modify the addStudent method to insert validation code -

[code language="java"]
@ActionMapping(params = "myaction=addStudent")
public void addStudent(@ModelAttribute("student") StudentImpl student, BindingResult result, ActionResponse actionResponse)
{
studentValidator.validate(student, result);
if(result.hasErrors())
{
actionResponse.setRenderParameter("myaction", "addStudentForm");
}
else
{
try {
long studentId = CounterLocalServiceUtil.increment(Student.class.getName());
student.setPrimaryKey(studentId);
StudentLocalServiceUtil.addStudent(student);
} catch (SystemException e) {
e.printStackTrace();
}
}
}
[/code]

c. modify the showAddStudentForm method -

[code language="java"]
@RenderMapping(params = "myaction=addStudentForm")
public String showAddStudentForm(Model model) throws SystemException
{
System.out.println("showAddStudentForm Called !!!!!!!!!!!");
return "addStudent";
}
[/code]

d. Add the following new method -

[code language="java"]
@ModelAttribute("student")
public Student getCommandObject() {
return new StudentImpl();
}
[/code]

3. make changes in addStudent.jsp file to display error messages-

[code language="html"]
<form:form action="<%=AddStudentURL.toString()%>" method="post" commandName="student">
<table>
<form:errors path="*" />
<tr><td>Name :</td> <td><form:input path="name" /></td></tr>
<tr><td colspan="2"><form:errors path="name"></form:errors></td></tr>
<tr><td>Subject:</td> <td><form:input path="subject" /></td></tr>
<tr><td colspan="2"><form:errors path="subject"></form:errors></td></tr>
<tr><td colspan="2"><input type="submit" value="Save" /></td></tr>
</table>
</form:form>
[/code]

4. Add the following snippet to myContext.xml file

[code language="xml"]
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language_en</value>
</list>
</property>
</bean>
[/code]

5. Add an entry to portlet.xml file -

[code language="xml"]
<resource-bundle>content.Language_en</resource-bundle>
[/code]

6. create new file named Language_en.properties with following content in src/content folder -

[code language="java"]
required.name = name is required!
required.subject = subject is required!
[/code]

We are done with all the changes required!