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 11:36:00 GMT

Whoops..... didn't read your whole mail properly.

What I should have said, is that you must make your properties into methods
because Velocity doesn't work with properties.
e.g.

class MenuItem {
    private String Name;
    private String Page;

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

You will notice that I have used "getName" and "getPage" - this is force of
habit on my part but also serves to illusrate that velocity will resolve
these to "Name" and "Page" in the context parser.

Steve (fool on the hill)

-----Original Message-----
From:
velocity-user-return-16313-sohara=pivotal-solutions.co.uk@jakarta.apache
.org
[mailto:velocity-user-return-16313-sohara=pivotal-solutions.co.uk@jakart
a.apache.org]On Behalf Of Steve O'Hara
Sent: 04 July 2005 11:50
To: Velocity Users List; Richard Nienaber
Subject: RE: Newb: Having trouble getting property and method values



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






---------------------------------------------------------------------
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