Spring mvc is easy to get along with, unless we get the basic flow right. We end up wasting lot of time figuring out the flow of control/binding of errors for validation/etc.
Spring portlet mvc validation
Spring servlet mvc validation
<property name="registrationService" ref="registrationService"/>
<property name="formView"><value>common/manageprofile</value></property>
<property name="commandClass"><value>com.TEST.web2.model.DpsUser</value></property>
<property name="validator" ><ref bean="userValidator"/></property>
<property name="successView"><value>common/manageprofile</value></property>
</bean>
<spring:bind path="command.confirmPassword">
<c:out value="${status.expression}"/> <input name=confirmPassword type="password" style="width:200px; " onKeyUp="document.getElementById('cpw').value=this.value" value="<c:out value="${status.value}"/>">
<font > <c:out value="${status.errorMessage}"/> </font>
</spring:bind>
Spring portlet mvc validation
- extend AbstractCommandController or SimpleFormController
- In case of AbstractCommandController implement the method handleRenderView(req,res,command,errors)
- Important thing to note is that you should return the ModelAndView as new ModelAndView(jspPage, errors..getModel());
- Note the method handleRenderView() is called even if validation fails. Hence we have to return back the command object having errors using errors.getModel().
Spring servlet mvc validation
- Implemented a simple form controller.
class ManageProfileController extend SimpleFormController
{
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
{
Map modelMap = new HashMap();
return new ModelAndView(forwardJSP, modelMap);
}
}
{
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
{
Map modelMap = new HashMap();
return new ModelAndView(forwardJSP, modelMap);
}
}
- The configuration in *-servlet.xml is as below (assume dispatcher servlet is configured already)
<bean id="manageProfileController" class="com.TEST.web2.portlet.controller.common.ManageProfileController">
<property name="registrationService" ref="registrationService"/>
<property name="formView"><value>common/manageprofile</value></property>
<property name="commandClass"><value>com.TEST.web2.model.DpsUser</value></property>
<property name="validator" ><ref bean="userValidator"/></property>
<property name="successView"><value>common/manageprofile</value></property>
</bean>
- On validation error, in servlet mvc, the onSubmit() is not called and the command object is available with errors.
- To extract the errors in JSP use:
<spring:bind path="command.confirmPassword">
<c:out value="${status.expression}"/> <input name=confirmPassword type="password" style="width:200px; " onKeyUp="document.getElementById('cpw').value=this.value" value="<c:out value="${status.value}"/>">
<font > <c:out value="${status.errorMessage}"/> </font>
</spring:bind>