i’m a beginner in Spring MVC.So i didn’t understand the flow of control is going from controller class to training-servlet.xml and vice-versa.
contextConfigLocation file(training-servlet.xml) is explained as :
<beans:bean id="userService" class="com.my.control.UserServiceImpl" />
<beans:bean name="/userRegistration.htm" class="com.my.control.HomeController">
<beans:property name="validator">
<beans:bean class="com.my.validations.HomeValidations" />
</beans:property>
<beans:property name="userService" ref="userService"></beans:property>
<beans:property name="formView" value="userForm"></beans:property>
<beans:property name="successView" value="userSuccess"></beans:property>
</beans:bean>
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
My Controller class extends SimpleFormController and is :
public class HomeController extends SimpleFormController {
private UserService userService;
public HomeController() {
setCommandClass(User.class);
setCommandName("user");
}
public void setUserService(UserService userService) {
this.userService = userService;
}
protected ModelAndView onSubmit(Object command) throws Exception {
System.out.println("Hai Inside");
User user = (User) command;
userService.add(user);
return new ModelAndView("userSuccess","user",user);
}
}
Please help me to understand the flow lies between model and controller.
The XML is only used at startup time. Spring uses your configuration file to create an instance of each bean specified within it, and connect its dependencies. That means that once you start your webapp in your web container, the controller’s userService field will be populated with a bean supplied by the application context. Spring MVC goes a little further to handle transforming an HttpServletRequest into a method invocation of the onSubmit() method
in your controller bean.
So there’s really no flow from the controller to the XML file, rather from one bean to another.