I have received some general Spring MVC questions regarding my Integrating Spring into your Weblogic Portal post. So in this post I will briefly describe a simple Spring Portlet MVC example along with some of the important annotations that can be used when developing Spring Controllers with version 2.5 or greater.
Controller
By annotating your controller with @Controller and adding your controller's package to the context:component-scan tag in your [portlet-name]-portlet.xml your controller will be auto-detected and your methods with the @RequestMapping annototation will be automatically mapped to their appropriate view. The context:component-scan tag can also be used in the applicationContext.xml to allow Spring find services or client code that is annotated with @Component. Then by using the @Resource annotation in your controller the Spring container will automatically inject an instance of the resource into your controller upon initialization. As I am doing here with the UserClient.
The @ModelAttribute annotation can be used in two ways.
- First, it can be used as a parameter to a method to pass the entire model Object from the view to a handler method.
- Secondly, it can be used on a method to hold data for the view. It works great for populating lists for drop-downs to be displayed in the view.
Here I am putting a List of User Objects in the 'users' model attribute to use to populate a select list in my view.
As mentioned earlier, the @RequestMapping annotation is how Spring (really the DispatcherPortlet) determines what method to call on a given request. The request parameter is just a name/value pair. If your portlet is called without any request parameters the default method is the one that is invoked.
Here I have a default show() method that doesn't take any RequestMapping params, it just instantiates the model Object, adds it to the ModelMap, and returns a String. The String being returned is the path/name to the jsp file for the view.
A couple others items to note; a render request handler can take RenderRequest and RenderResponse parameters and an action request handler can take ActionRequest and ActionResponse parameters. Often times an action request handler will have a void return type and set a render parameter on the response object to route the response to the correct render request handler (as seen in the activateUser() method below). The activateUser() method has a RequestMapping params of 'do=activateUser' and it also expects an 'id' as a request parameter.
You can see from just these three method signatures the flexibility you are given when using annotation based controllers. Again see the Portal MVC Documentation link below for details regarding what is allowed.
Model
The model object for the view is just a simple POJO with the standard bean setters and getters.
View
The jsp shown below makes use of the Spring form tag. The Spring form tag contains two important attributes. First, the modelAttribute specifies the name of the model Object used for the view. Secondly, the action attribute specifies the action that is performed when the page is submitted. Many of the Spring tags contain the path attribute which is used to reference a property of the Model Object for data binding. This portlet displays a select list of users and a submit button. Once submitted the selected user is activated and the User Object is stored in the user model attribute to then be rendered.
Reference