Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Friday, December 31, 2010

Highlevel Spring Portlet MVC Overview

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

Tuesday, May 18, 2010

Integrating Spring Portlet MVC into your Weblogic Portal

Switching out your Weblogic Page Flow Controllers in favor of Spring Controllers is easier than you might think. Here are just a few steps to help the integration.

Dependencies
The latest production release version of Spring is version 3.0.2. In this version they have broken the release into about 20 different jar files. The libraries listed below are those that you will need to include in the WEB-INF/lib your project:
  • org.springframework.asm-3.0.2.RELEASE.jar
  • org.springframework.beans-3.0.2.RELEASE.jar
  • org.springframework.context-3.0.2.RELEASE.jar
  • org.springframework.context.support-3.0.2.RELEASE.jar
  • org.springframework.core-3.0.2.RELEASE.jar
  • org.springframework.expression-3.0.2.RELEASE.jar
  • org.springframework.web-3.0.2.RELEASE.jar
  • org.springframework.web.portlet-3.0.2.RELEASE.jar
  • org.springframework.web.servlet-3.0.2.RELEASE.jar
You'll also want to add the spring.tld and spring-form.tld in your WEB-INF/tlds directory. That way you can switch out your netui tags for the equivalent spring tags in your jsps.

Configuration
To integrate Spring into your portal application there are a few required configuration items that are needed.
  • web.xml - The listener and application context-param below basically just tell the Spring container where to find your applicationContext configuration file. This servlet is what converts the PortletRequest/PortletResponse to HttpServletRequest/HttpServletResponse for the DispatcherPortlet


  • applicationContext.xml - The applicationContext.xml is where you define beans and configuration items that will be shared by all the portlet within your web application. Here you can specify packages to scan for resources (clients/services) that will be called from your controllers. Here you also define a viewResolver bean so that Spring can resolve the views. Other application-wide beans can also be defined here, such as, a message codes resolver for displaying messages and labels and an exception resolver for error handling.
Portlet
Once your Spring Controller has been created the next step is to create the .portlet file.
  • Within Weblogic Workshop right click in the Package Explorer/Navigation panel and select New -> Portlet and give your .portlet file a name and location and click Next.

  • Then the Portlet Wizard will appear and prompt you to select a portlet type. Click the Java Portlet radio button to create a JSR 168 compliant portlet and click Next.

  • Next you will need to give a a title, label, portlet name, and a Class name and click Next.
    (The Definition Label and Portlet Name will generally always be the same, which will be the same as your [portlet-name]-portlet.xml configuration file. The Class Name will always be org.springframework.web.portlet.DispatcherPortlet. The information entered here is automatically added to the web application's portlet.xml file.)

  • Finally, click Create to create the portlet.

  • Upon creation of your .portlet file an entry similar to the one below will be automatically added to your portlet.xml file.












  • Once your .portlet file is created you can open your .portal file within Weblogic Workshop and your new Portlet should display in the Design Palette panel. You can then easily drag your portlet onto any Book or Page you wish.

That's it. Now just redeploy your web app.