Tile Controllers

 < Day Day Up > 



If you feel you are putting too much Java code into your Tile layout, or you have to put the same Java code into every Action that forwards to a page that uses a particular Tile layout, then you should use a Tile Controller. You can specify a Controller class that is called before a Tile is inserted by using the controllerClass attribute:

 <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> <tiles:insert definition="siteLayoutDef5"               controller>     <tiles:put name="content" value="indexContent5.jsp" /> </tiles:insert> 

The Controller class is similar to an Action. You can map model objects into scope so that the Tile can display the items.

To write a Tile Controller, follow these steps:

  1. Create a class that implements org.apache.struts.tiles.Controller.

  2. Implement the perform method.

  3. In the perform method, do something with the model and map the results into scope so the Tile can use it.

Listing 13.9 contains an example of implementing a Controller.

Listing 13.9: ch13.SimpleController.

start example
 package ch13; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.tiles.ComponentContext; import org.apache.struts.tiles.Controller; import org.apache.struts.tiles.beans.MenuItem; import org.apache.struts.tiles.beans.SimpleMenuItem; import java.util.ArrayList; import java.util.List; /**  * @author rhightower  */ public class SimpleController implements Controller{     private MenuItem createMenuItem(String label, String link){               SimpleMenuItem item = new SimpleMenuItem();               item.setLink(link);               item.setValue(label);               return item;     }     private List getLinks(){               List list = new ArrayList();               list.add(createMenuItem("Home",                      "/index.html"));               list.add(createMenuItem("Rick's",             "http://www.rickhightower.com"));               list.add(createMenuItem("Trivera",                      "http://www.triveratech.com"));               return list; }        /* (non-Javadoc)         *        */         public void perform(ComponentContext context,                             HttpServletRequest request,                             HttpServletResponse response,                          ServletContext servletContext)                        throws ServletException, IOException {             List items = (List)getLinks();             context.putAttribute("items",items);        } } 
end example

Notice that the perform method is passed the component context. The component context holds all of the attributes of Tile scope. Putting things in the component context places them in Tile scope. In this simple example, we call getLinks, which returns a simple list of MenuItems that we map into Tile scope. A real-world example would likely talk to the model—perhaps a facade that communicates with a database to look up links specific to the type of user logged into the system.

start sidebar

You can instead use an Action as the Controller for the Tile. To do this, you have to specify the path of the Action with the controllerUrl attribute.

end sidebar



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net