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.

Tuesday, March 23, 2010

Adobe Online Forms and Your Mac

I recently worked on a project where we converted PDF files to web forms using Adobe LifeCycle Designer. Then using the iText Java library we added buttons to Save, Submit, Print, and Cancel the forms. Everything worked great using IE and Firefox on a Windows machine; however, it was a different story on a Mac. The buttons were basically useless when using Firefox or Safari on a Mac running Snow Leopard. So once a form was open there was no way to navigate away from the form short of using the browser's back button.

The problem turned out to be a compatibility issue between the 32-bit Adobe browser plug-in and the 64-bit Safari web browser.
The solution is to run Safari in the 32-bit mode. To do this open Finder and control + Click on the Safari.app under Applications and select Get Info. An image similar to the one below will display and simple click the Open in 32-bit mode box, close out of the window and relaunch Safari.

Tuesday, July 14, 2009

easy spreadsheet processing with JExcel

On a recent project we had the need to process Microsoft Excel spreadsheets. We chose to use an API, called JExcel, to read, write, and process Excel files. This post will focus on reading and processing an Excel spreadsheet.

To start you'll need to create a Workbook, which can be done by reading from a File or InputStream. Next, get each Sheet to process by calling .getSheets(). Need to be sure to close the workbook to free up resources by calling .close() once processing is complete.


Once you have a Sheet you can get each Row, which will be an Array of Cells and then get the contents of each Cell as a String. The API also gives you the ability to get a cell's value of a particular type (i.e. Number, Date, etc.). See the API documentation for examples.


*** Note - One downfall of the current version (2.6.10) is that it cannot read Microsoft 2007 Excel file version (.xlsx). Hopefully the next release will address this issue.

Monday, February 9, 2009

jboss portal redirect

The purpose of this post is to describe how you can redirect a user to a specific landing page within a JBoss Portal application. The jboss-service.xml file in your JBOSS_HOME/server/default/deploy/jboss-portal.sar/META-INF directory contains the managed bean definitions and registration of the beans that are used. There is one entry that is of importance to accomplish the redirect, which is the DeafultPortalCommandFactory. This factory is called when every request for your portal is made. Its main objective is to check what the next command is, and if it does not have one, it will render/return the default portal page in your -object.xml.

To change this behavior you can extend the org.jboss.portal.core.model.portal.DeafultPortalCommandFactory class and override the doMapping() method to invoke the desired redirect (which can be a redirect to another portlet other than the default or an entirely different portal). You may want to stick a flag in the session to notate that the session has been redirected to avoid a redirect loop. Below is the doMapping() method of com.demo.NewDefaultPortalCommandFactory.java.

Then just package your new class as a JBoss component (.sar file) and register it within the jboss-service.xml. Your new JBoss component will have a packaging structure similar to the one below.
new-portal.sar/
com.demo.NewDefaultPortalCommandFactory
META-INF/
jboss-service.xml
You will need to define your new DefaultPortalCommandFactory within the jboss-service.xml inside your portal.sar.

Finally, you simply need to comment out (or modify) the registration of the DefaultPortal and add an entry your new Command Factory in the jboss-service.xml file in the jboss-portal.sar/META-INF.

Tuesday, January 20, 2009

available memory is low

If you have used Eclipse as your IDE you have probably seen the 'Available Memory Is Low' error message. The error itself tells you exactly what the problem is and what the recommended memory parameters should be. However, it doesn't tell you where you need to make those changes and how you can verify what your current configuration is.To check your current memory settings on a Mac go to Eclipse -> About Eclipse Platform then click Configuration Details and scroll down just a little and you will be able to see your settings (which may look something like the following).

-Xms40m
-Xmx256m
-XX:MaxPermSize=256m


The location of the file you need to edit is located in your Eclipse installation directory. Right click (or Control-Click) on the Eclipse.app file and select Show Package Contents. Then traverse to Contents -> MacOS and open the eclipse.ini file.It is within the eclipse.ini file where you can update the memory settings as recommended by the error messsage.

-Xms128m
-Xmx512m
-XX:PermSize=64m
-XX:MaxPermSize=256m


Now you just need to restart Eclipse and you can verify your new configuration settings by again accessing Eclipse -> About Eclipse Platform then clicking Configuration Details. Now, you should no longer see the 'Available Memory Is Low' messages.

The image reflections in this post were made at http://www.reflectz.net/ (check it out, it's pretty cool!)

Wednesday, January 14, 2009

my first time

Tonight I went to the Richmond Java User Group (RJUG) meeting to attend Jared Richardson's, "Career 2.0 - Take Control of Your Life" presentation. I highly recommend anyone looking to get more out of their career and their life. His presentation inspired me to start this blog. It made me realize that if I want to feel fulfilled with my career I need to take the responsibility of educating myself and sharing what I have learned. This blog will give me a medium to do just that.

My plan is to make monthly posts on solutions to problems I faced and/or new technologies I learned over the month. Hopefully, I can stick with it.