Monday, August 4, 2008

Struts and Model View Controller MVC: Struts Best Practices

Struts and MVC

Struts enforces good Model View Controller development.

An actions class implements control logic, delegating real work to JavaBeans and EJBs, and then figure out where to forward a user when the tough work is done.

Emphasizing the enforcement the MVC philosophy, you will notice that the return type of the exectue method is an ActionForward object. The ActionForward object represents the jsp or html page to be called with the intention of generating a view for the client.

The first line of code in out Action class creates a blank ActionForward object creatively named forward. In our control logic, we then initialize the forward based upon the language the user is speaking. If the user prefers Spanish, we will send them to the spanishpage. If the user speaks french, we will forward them to the frenchpage. If the user doesn't speak french or spanish, we will force english down their throat.

Now there is a little Vodoo going on behind the scenes as we forward to these French, Spanish and English pages. After all, in our TryLingualAction class, we forward to a frenchpage, spanishpage and englishpage, yet, on our filesystem, our pages are named frenchspeaker.jsp, spanishspeaker.jsp and englishspeaker.jsp respectively. It’s not an error in my code. Quite the contrary – it is completely intended.

If you look at the method signature of the execute method, you'll notice that the first object passed in is an instance of an ActionMapping, creatively named mapping. An ActionMapping is used to associate simple text strings, such as "englishpage," with actual living and breathing JSP and html pages on the filesystem. Remember that our jsp wasn't called englishpage, but instead, it was called englishspeaker.jsp

So the question is, how does this magical mapping object know that a word like "englishpage" maps to the englishspeaker.jsp? Well, it's all about the struts-config.xml file.


Exploring the struts-config.xml File

The struts-config.xml contains a very important section called ‘action-mappings’ These action-mappings contain a listing of all the Action classes in your application, along with the url the client will use to invoke the action.

type="com.pulpjava.action.TryLingualAction">

In the action-mapping for the TryLingualAction, you can see that the path is /tryLingual. This means a client can invoke the action by using a url such as:

http://www.pulpjava.com/PulpJavaWeb/tryLingual.do

Furthermore, there are local mappings within the action-mapping.

Inside the action-mapping for the TryLingualAction, you can see a forward associating the string “englishpage” to the file “englishspeaker.jsp” Similar mappings exist for spanishpage and frenchpage.

In our TryLingualAction class, we can write the following code:

forward = mapping.findForward("englishpage");

“englishpage” gets mapped to englishspeaker.jsp, and when the execute method of the TryLingualAction class returns this forward, the client will be served up the content generated by the englishpeaker.jsp file.

The mapping servers several purposes. First of all, it eliminates having to enter the names of jsp files in your code. If your files change, you only need to change your struts-config.xml file, not any Java code.

Furthermore, due to the fact that the forwards for englishpage, spanishpage and frenchpage are all local forwards, which means they are declared within the TryLingualAction entry in the struts-config.xml file, no other Action class can access these three jsp pages using their colloquial mappings.


No comments: