velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve O'Hara" <soh...@pivotal-solutions.co.uk>
Subject RE: Newb: Having trouble getting property and method values
Date Mon, 04 Jul 2005 10:50:25 GMT

The Name and Page elements are methods and so must be expressed as such in
your template e.g.

$item.Name()
$item.Page()

BTW you only need to use the curly braces if your reference might be
ambiguous


Steve

-----Original Message-----
From:
velocity-user-return-16310-sohara=pivotal-solutions.co.uk@jakarta.apache
.org
[mailto:velocity-user-return-16310-sohara=pivotal-solutions.co.uk@jakart
a.apache.org]On Behalf Of Richard Nienaber
Sent: 04 July 2005 08:37
To: velocity-user@jakarta.apache.org
Subject: Newb: Having trouble getting property and method values


Here's my ServLet code:
    protected Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context context) throws IOException {
        //set mainpage
        context.put("MainPage", "/test/mainpage.jsp");

        //create three menus
        Vector menus = new Vector();

        menus.add(new MenuItem("Admin Page", "/test/admin.jsp"));
        menus.add(new MenuItem("Internet Options", "/test/internet.jsp"));

        context.put("menus", menus);

        try {
            return getTemplate("templates/mainpage.vm");
        } catch (ResourceNotFoundException rnfe) {
            logger.error("TEMPLATE NOT FOUND");
        } catch (ParseErrorException pee) {
            logger.error("PARSEREXCEPTION", pee);
        } catch (Exception e) {
            logger.error("Exception", e);
        }

        return null;

    }
}

class MenuItem {
    public String Name;
    public String Page;

    public MenuItem(String name, String page) {
        this.Name = name;
        this.Page = page;
    }
}

Here's my template code (mainpage.vm):
<HTML>
<BODY>
Welcome to SatDaemon! Anytime you want to get back to the original
page click <A href="$MainPage">here</a>
<br>
<br>
<table>
#foreach( $item in $menus )
      <tr>
        <td>
		  <a href="${item.Page}">${item.Name}</a>
        </td>
      </tr>
#end
</table>
</body>
</HTML>

This is what I get back from the server:
<HTML>
<BODY>
Welcome to SatDaemon! Anytime you want to get back to the original
page click <A href="/test/mainpage.jsp">here</a>
<br>
<br>
<table>
      <tr>
        <td>
		  <a href="${item.Page}">${item.Name}</a>
        </td>

      </tr>
      <tr>
        <td>
		  <a href="${item.Page}">${item.Name}</a>
        </td>
      </tr>
</table>
</body>
</HTML>

It's obviously picking up on the vector but I just can't seem to get
the values out. This seems like such an obvious problem but I just
can't get my head around it.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org






---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message