You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nikola Milutinovic <al...@yahoo.com> on 2007/06/22 10:46:53 UTC

How to write this elegantly in JSP/EL

Hi all.

This is not really TC related, but more JSP and EL question, but I thought I'd ask.

I would like to have an elegant way of using constants from Java classes in EL. This is a relatively good illustration of what I am talking about. Take, for instance, a Spring controller. It has a method where the programmer can bind objects to model-and-view, which basically binds it to a request or session scope. It is very similar to a plain Servlet doing "request.setAttribute( "name", value )".

public class SomeController extends SimpleFormController {

    public static final String DATA_PHOTO_WIDTH = "photoWidth";
    public static final String DATA_PHOTO_HEIGHT = "photoHeight";

    ...

    protected referenceData( ... ) {

        ...

        modelAndView.put( DATA_PHOTO_WIDTH, photoWidth );
       modelAndView.put( DATA_PHOTO_HEIGHT, photoHeight );

        ...

    }

...

}


And then in JSPF or JSP:



<img src="${imageUrl}" alt="Author photo" height="${photoHeight}" width="${photoWidth}" />


Notice that the EL variable name is the same as that constant. So, I have 2 places to keep in synch. If I wrote that in a scriptlet, it would be correct, from the point of "do not duplicate code":



<%= request.getAttribute( com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH ) %>

Now, how could I do the same in EL?


Something like (tongue in cheek):

width="${=com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH}"

Did I miss something crucial in EL?

Nix.




       
____________________________________________________________________________________
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing.
http://new.toolbar.yahoo.com/toolbar/features/mail/index.php

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to write this elegantly in JSP/EL

Posted by ben short <be...@benshort.co.uk>.
I think the following should do you

<img src="${imageUrl}" alt="Author photo" height="<c:out
value="${DATA_PHOTO_HEIGHT}"/>" width="<c:out
value="${DATA_PHOTO_WIDTH}"/>" />

On 6/22/07, Johnny Kewl <jo...@kewlstuff.co.za> wrote:
>
> Hi Nikola, never done it, but I think you have to override the resolver.
>
> ie instead of the standard resolver, which looks for standard properties or
> custom classes declared thru XML.... yours will tie to a class in your app.
> Managed to find an ok article.... maybe its enuf for you to find the
> relevent code in spring, and steal it ;)
>
> http://today.java.net/pub/a/today/2006/03/07/unified-jsp-jsf-expression-language.html
>
> ----- Original Message -----
> From: "Nikola Milutinovic" <al...@yahoo.com>
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Sent: Friday, June 22, 2007 10:46 AM
> Subject: How to write this elegantly in JSP/EL
>
>
> > Hi all.
> >
> > This is not really TC related, but more JSP and EL question, but I thought
> > I'd ask.
> >
> > I would like to have an elegant way of using constants from Java classes
> > in EL. This is a relatively good illustration of what I am talking about.
> > Take, for instance, a Spring controller. It has a method where the
> > programmer can bind objects to model-and-view, which basically binds it to
> > a request or session scope. It is very similar to a plain Servlet doing
> > "request.setAttribute( "name", value )".
> >
> > public class SomeController extends SimpleFormController {
> >
> >    public static final String DATA_PHOTO_WIDTH = "photoWidth";
> >    public static final String DATA_PHOTO_HEIGHT = "photoHeight";
> >
> >    ...
> >
> >    protected referenceData( ... ) {
> >
> >        ...
> >
> >        modelAndView.put( DATA_PHOTO_WIDTH, photoWidth );
> >       modelAndView.put( DATA_PHOTO_HEIGHT, photoHeight );
> >
> >        ...
> >
> >    }
> >
> > ...
> >
> > }
> >
> >
> > And then in JSPF or JSP:
> >
> >
> >
> > <img src="${imageUrl}" alt="Author photo" height="${photoHeight}"
> > width="${photoWidth}" />
> >
> >
> > Notice that the EL variable name is the same as that constant. So, I have
> > 2 places to keep in synch. If I wrote that in a scriptlet, it would be
> > correct, from the point of "do not duplicate code":
> >
> >
> >
> > <%= request.getAttribute(
> > com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH
> >  ) %>
> >
> > Now, how could I do the same in EL?
> >
> >
> > Something like (tongue in cheek):
> >
> > width="${=com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH}"
> >
> > Did I miss something crucial in EL?
> >
> > Nix.
> >
> >
> >
> >
> >
> > ____________________________________________________________________________________
> > Get the Yahoo! toolbar and be alerted to new email wherever you're
> > surfing.
> > http://new.toolbar.yahoo.com/toolbar/features/mail/index.php
> >
> > ---------------------------------------------------------------------
> > To start a new topic, e-mail: users@tomcat.apache.org
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to write this elegantly in JSP/EL

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
Hi Nikola, never done it, but I think you have to override the resolver.

ie instead of the standard resolver, which looks for standard properties or 
custom classes declared thru XML.... yours will tie to a class in your app.
Managed to find an ok article.... maybe its enuf for you to find the 
relevent code in spring, and steal it ;)

http://today.java.net/pub/a/today/2006/03/07/unified-jsp-jsf-expression-language.html

----- Original Message ----- 
From: "Nikola Milutinovic" <al...@yahoo.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Friday, June 22, 2007 10:46 AM
Subject: How to write this elegantly in JSP/EL


> Hi all.
>
> This is not really TC related, but more JSP and EL question, but I thought 
> I'd ask.
>
> I would like to have an elegant way of using constants from Java classes 
> in EL. This is a relatively good illustration of what I am talking about. 
> Take, for instance, a Spring controller. It has a method where the 
> programmer can bind objects to model-and-view, which basically binds it to 
> a request or session scope. It is very similar to a plain Servlet doing 
> "request.setAttribute( "name", value )".
>
> public class SomeController extends SimpleFormController {
>
>    public static final String DATA_PHOTO_WIDTH = "photoWidth";
>    public static final String DATA_PHOTO_HEIGHT = "photoHeight";
>
>    ...
>
>    protected referenceData( ... ) {
>
>        ...
>
>        modelAndView.put( DATA_PHOTO_WIDTH, photoWidth );
>       modelAndView.put( DATA_PHOTO_HEIGHT, photoHeight );
>
>        ...
>
>    }
>
> ...
>
> }
>
>
> And then in JSPF or JSP:
>
>
>
> <img src="${imageUrl}" alt="Author photo" height="${photoHeight}" 
> width="${photoWidth}" />
>
>
> Notice that the EL variable name is the same as that constant. So, I have 
> 2 places to keep in synch. If I wrote that in a scriptlet, it would be 
> correct, from the point of "do not duplicate code":
>
>
>
> <%= request.getAttribute( 
> com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH 
>  ) %>
>
> Now, how could I do the same in EL?
>
>
> Something like (tongue in cheek):
>
> width="${=com.libris4you.books.maintenancepanel.AuthorTabController.DATA_PHOTO_WIDTH}"
>
> Did I miss something crucial in EL?
>
> Nix.
>
>
>
>
>
> ____________________________________________________________________________________
> Get the Yahoo! toolbar and be alerted to new email wherever you're 
> surfing.
> http://new.toolbar.yahoo.com/toolbar/features/mail/index.php
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to write this elegantly in JSP/EL

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 6/22/07, Nikola Milutinovic <al...@yahoo.com> wrote:
> Hi all.
>
> This is not really TC related, but more JSP and EL question, but I thought I'd ask.

I ask JSP, EL and JSTL questions here

http://jakarta.apache.org/taglibs/

or

http://forum.java.sun.com/forum.jspa?forumID=45

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org