Step 2: The heavy lifting in this web application is done by a controller class. This is an ordinary java class or bean that extends org.springframework.web.servlet.mvc.AbstractController. We override the handleRequestInternal method. In this method, you would do the things necessary to handle the request which may include for example reading from a database.
The method returns a org.springframework.web.servlet.ModelAndView object which encapsulates the name of the view and any data (model) that needs to be displayed by the view. ModelAndView holds data as name value pairs.This data is later made available to the view. If the view is a jsp, then you can access the data using either jstl techniques or by directly querying the Request object. The code for our controller is shown below:
view source
print?
1
public class SpringMVCController extends AbstractController {
2
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
3
ModelAndView mview = new ModelAndView("springmvc") ;
4
mview.addObject("greeting", "Greetings from SpringMVC") ;
5
mview.addObject("member1", new Member("Jonh","Doe",
6
"1234 Main St","Pleasanton","94588","[email protected]","1234")) ;
7
return mview ;
8
}
9
}
The name of the view springmvc is passed in to the constructor of ModelAndView. The addObject method invocations add 2 model objects, “greeting” and “member1”. Later you will see how the view can retrieve the objects and display them.
|