Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences

Edit this page


Referenced by
LeftMenu




JSPWiki v2.2.33

[RSS]


Hide Menu

StrutsMenu


I did some refactorings on Struts Menu in the last few days and thought I'd document them here as it's a bit more convenient than via e-mail. Also, I have a couple issues that might be better exposed here vs. a mailing list.

Refactorings

  • Migrated to Maven for building and deploying.
  • Renamed package from com.fgm.menu to net.sf.navigator.
  • Checked in as a new module on SourceForge called "navigator". I liked the name and thought it would be a bit cleaner than "struts-menu" going forward. I'd like to use the same project at SF, just have a new name for the package/releases/files.
  • Added the ability to specify dynamic parameters in menu-config.xml. Example:
  page="/velocity.jsp?test=${test} 
In this example, the variable 'test' is searched for using pageContext.findAttribute("test"). You could easily set this parameter using JSTL and <c:set var="test" value="value" />. I also added support for request parameters if not found in the pageContext.
  • Added a VelocityMenuDisplayer which puts a bunch of stuff in a VelocityContext for creating of menus with Velocity. This might make for an easy way to use this navigation system and build it from a database. Just put your Tree (or whatever you want to use) into the request, and grab it in the template. Here are all the current variables exposed:


    context.put("formatter"new VelocityFormatter(context));
    context.put("now", Calendar.getInstance().getTime());
    context.put("ctxPath", request.getContextPath());

    // see if a name and property were passed in
    if (!StringUtils.isEmpty(name)) {
        Object val1 =
            RequestUtils.lookup(pageContext, name, null, null);

        if (val1 != null) {
            context.put(name, val1);
        }
    }

    // request-scope attributes
    Enumeration enum = request.getAttributeNames();

    while (enum.hasMoreElements()) {
        String name = (Stringenum.nextElement();
        Object value = request.getAttribute(name);
        context.put(name, value);
    }

    context.put("request", request);
    context.put("session", request.getSession());
    context.put("menu", menu);
    context.put("displayer"this);

Here is a sample velocity template for rendering a simple menu. It shouldn't be too hard to re-create the existing using Velocity templates.

## Evaluates other macros.
#macro(eval $_macro)$_macro#end

#macro( displayMenu $menu $level )
  #if ($menu.components.size() > 0)
    ## display top menu
    #menuItem($menu $level)
    #foreach ($menu in $menu.components)
      #local ($menu $level)
        #set ($level = $level+1)
        #if ($menu.components.size() > 0) 
          #eval("#displayMenu($menu $level)")
        #else
          #menuItem($menu $level)
        #end
      #end
    #end
  #else
    #menuItem($menu $level)
  #end
#end
  
#macro( menuItem $menu $level )
  #foreach ($i in [0..$level])
    &amp;nbsp;&amp;nbsp;
  #end
  #if ($menu.url) 
    <a href="$menu.url" title="$menu.title">
        $menu.title</a>
  #else 
    $menu.title
  #end
  <br />
#end

#displayMenu($menu 0)

This is configured in your JSP using:

<menu:useMenuDisplayer name="Velocity" config="/table.html"
  bundle="org.apache.struts.action.MESSAGE">
  <menu:displayMenu name="indexMenu"/>
</menu:useMenuDisplayer>

Where config points to a file relative to your webapp. You can also use config="table.html" where table.html is under WEB-INF/classes.

Issues

  • I don't know if its Velocity or the tag library, but someone is caching the templates and the only way to get them to refresh is to restart Tomcat. I'd like to be messing with the JSP and it's template on Tomcat and just refresh them in the browser (after saving). It'd sure cut down on development time. Lance: This probably is Velocity, have you checked your velocity properties to make sure cache=false? And try adding this: velocimacro.library.autoreload=true. Ivan: I recently found out that Tomcat 4.1.29 (and probably others) caches the content of resources loaded through getResourceAsStream() (very confusing), this may be causing your problems. As a work around my reloadable configuration class was changed to simply getResource() and then opening the stream on the URL.
  • There seems to be an issue when you have two menus (menu:useMenuDisplayer tag) on one page. The second menu seems to try and use parts of the first table's config. Wierd. Lance: I've seen some scope issues like this on Roller. There are some optional flags documented in the Users Guide that may help with this - though it isn't altogether clear which does what.

UPDATE: I solved all of these issues by implementing Velocity's WebappLoader rather than the one from Roller. Here's the code I used to initialize it:


/**
 * Key used to access the ServletContext in the Velocity application attributes.
 */
public static final String SERVLET_CONTEXT_KEY = ServletContext.class.getName();
  
//~ Methods ================================================================

public void init(PageContext pageContext, MenuDisplayerMapping  mapping) {
  super.init(pageContext, mapping);
  this.pageContext = pageContext;

  // MR: Copied from VelocityViewServlet to initialize WebappLoader
  Velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY,  
                                   pageContext.getServletContext());
  
  // default to servletlogger, which logs to the servlet engines log
  Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
                       ServletLogger.class.getName());
  
  // by default, load resources with webapp resource loader
  Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp");
  Velocity.setProperty("webapp.resource.loader.class",  
                       WebappLoader.class.getName());

  // now all is ready - init Velocity
   try {
       ResourceBundle rb = ResourceBundle.getBundle("velocity");

       Properties props = new Properties();

       for (Enumeration keys = rb.getKeys();keys.hasMoreElements();) {
           String key = (Stringkeys.nextElement();
           props.put(key, rb.getString(key));
       }

       // only initialized the first time it's called, from:
       //  http://jakarta.apache.org/velocity/developer-guide.html
       //  it's ignored for subsequent calls
       Velocity.init(props);
   catch (Exception e) {
       log.error("Error initializing Velocity: " + e.getMessage());
       e.printStackTrace();
   }
}

TODO

  • Use XDoclet to generate the .tld file.
  • Unit Tests!!

Comments, suggestions or questions are most welcome. If not here, doing it on one of the mailing lists is probably most appropriate.

Update: I just thought of something that would be pretty cool. Allow the name in <menu:displayMenu name="indexMenu"/> to refer to the name of a tiles definition that defines a menu. That'd be slick!



Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:52:58 MST by MattRaible.