- 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().
- 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);
}
}
- 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:
No comments:
Post a Comment