You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Whittaker <mi...@ntlworld.com> on 2003/08/01 13:32:32 UTC

simple - import

How do you get Constants (or anything none scoped ie app/sess/req) into a
jsp for use in EL

I can do

<%@ page import="com.blah.Const" %>

<%= Const.MY_CONST %>

but not

<c:out value="${Const.MY_CONST}"/>

This probably demonstrates a fundamental none understanding!

TIA
--
Mike W


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


RE: simple - import

Posted by Mike Whittaker <mi...@ntlworld.com>.
All these ideas rely on the use of a single class/interface for declaring
constants.
I don't like this workaround to what SDK1.5 will solve.
My constants are in the right places, the classes to which they logically
belong.
I suppose with 1.5 I will be importing these constants to one class which I
can then make available as you suggest.
I think EL should have syntax to enable it to access statics

--
Mike W

>
>
>Alternatively, you could load this into session or application
>scopes and do
>this:
>
><c:out value="${sessionScope.constants.myVal}">
>or
><c:out value="${applicationScope.constants.myVal}">
>----- Original Message -----
>From: "David Chelimsky" <da...@chelimsky.org>
>To: "Struts Users Mailing List" <st...@jakarta.apache.org>;
><al...@law.columbia.edu>
>Sent: Friday, August 01, 2003 8:12 AM
>Subject: Re: simple - import
>
>
>> JSTL won't handle static methods. If you add a method to your Const class
>> that returns a Const instance (ideally a singleton so you don't keep
>> creating these), you can keep a Const on your ActionForm bean and set it
>in
>> your ActionForm. Then your JSP would look like this:
>>
>> <c:out value="${myForm.const.myVar}"/>
>>
>> and you wouldn't even need the import statement.
>>
>> ----- Original Message -----
>> From: "Alex Shneyderman" <al...@law.columbia.edu>
>> To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
>> Sent: Friday, August 01, 2003 6:34 AM
>> Subject: RE: simple - import
>>
>>
>> >
>> > <c:set var="yourVar"><%= Const.MY_CONST %></c:set>
>> >
>> > > -----Original Message-----
>> > > From: Mike Whittaker [mailto:mike_whittaker@ntlworld.com]
>> > > Sent: Friday, August 01, 2003 7:33 AM
>> > > To: Struts List
>> > > Subject: simple - import
>> > >
>> > >
>> > > How do you get Constants (or anything none scoped ie app/sess/req)
>> > into a
>> > > jsp for use in EL
>> > >
>> > > I can do
>> > >
>> > > <%@ page import="com.blah.Const" %>
>> > >
>> > >
>> > >
>> > > but not
>> > >
>> > > >
>> > > This probably demonstrates a fundamental none understanding!
>> > >
>> > > TIA
>> > > --
>> > > Mike W
>> > >
>> > >
>> > > ---------------------------------------------------------------------
>> > > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> > > For additional commands, e-mail: struts-user-help@jakarta.apache.org
>> >
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
>> >
>> >
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


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


Re: simple - import

Posted by David Chelimsky <da...@chelimsky.org>.
Alternatively, you could load this into session or application scopes and do
this:

<c:out value="${sessionScope.constants.myVal}">
or
<c:out value="${applicationScope.constants.myVal}">
----- Original Message -----
From: "David Chelimsky" <da...@chelimsky.org>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>;
<al...@law.columbia.edu>
Sent: Friday, August 01, 2003 8:12 AM
Subject: Re: simple - import


> JSTL won't handle static methods. If you add a method to your Const class
> that returns a Const instance (ideally a singleton so you don't keep
> creating these), you can keep a Const on your ActionForm bean and set it
in
> your ActionForm. Then your JSP would look like this:
>
> <c:out value="${myForm.const.myVar}"/>
>
> and you wouldn't even need the import statement.
>
> ----- Original Message -----
> From: "Alex Shneyderman" <al...@law.columbia.edu>
> To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
> Sent: Friday, August 01, 2003 6:34 AM
> Subject: RE: simple - import
>
>
> >
> > <c:set var="yourVar"><%= Const.MY_CONST %></c:set>
> >
> > > -----Original Message-----
> > > From: Mike Whittaker [mailto:mike_whittaker@ntlworld.com]
> > > Sent: Friday, August 01, 2003 7:33 AM
> > > To: Struts List
> > > Subject: simple - import
> > >
> > >
> > > How do you get Constants (or anything none scoped ie app/sess/req)
> > into a
> > > jsp for use in EL
> > >
> > > I can do
> > >
> > > <%@ page import="com.blah.Const" %>
> > >
> > >
> > >
> > > but not
> > >
> > > >
> > > This probably demonstrates a fundamental none understanding!
> > >
> > > TIA
> > > --
> > > Mike W
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>


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


Re: simple - import

Posted by David Chelimsky <da...@chelimsky.org>.
JSTL won't handle static methods. If you add a method to your Const class
that returns a Const instance (ideally a singleton so you don't keep
creating these), you can keep a Const on your ActionForm bean and set it in
your ActionForm. Then your JSP would look like this:

<c:out value="${myForm.const.myVar}"/>

and you wouldn't even need the import statement.

----- Original Message -----
From: "Alex Shneyderman" <al...@law.columbia.edu>
To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
Sent: Friday, August 01, 2003 6:34 AM
Subject: RE: simple - import


>
> <c:set var="yourVar"><%= Const.MY_CONST %></c:set>
>
> > -----Original Message-----
> > From: Mike Whittaker [mailto:mike_whittaker@ntlworld.com]
> > Sent: Friday, August 01, 2003 7:33 AM
> > To: Struts List
> > Subject: simple - import
> >
> >
> > How do you get Constants (or anything none scoped ie app/sess/req)
> into a
> > jsp for use in EL
> >
> > I can do
> >
> > <%@ page import="com.blah.Const" %>
> >
> >
> >
> > but not
> >
> > >
> > This probably demonstrates a fundamental none understanding!
> >
> > TIA
> > --
> > Mike W
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>


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


Re: simple - import

Posted by James Mitchell <jm...@apache.org>.
Easy way:
--------
You can always use the "unstandard" tags.  Works great for me:

<un:bind type="com.company.project.Const" field="SOME_CONST" var="yourVar"/>
<c:if test="${$yourVar == 'some value'}">
 <c:out value="${yourVar}"/>
</c:if>


Not so easy way:
---------------
(by Kris Schneider on taglib-users)

Put something like this in a startup servlet or a plug-in:

    public static Map getConstantFieldsAsMap(Class cls)
      throws IllegalAccessException {
        Field[] allFields = cls.getDeclaredFields();
        int numFields = allFields.length;
        Map propMap = new HashMap(numFields);
        for (int i = 0; i < numFields; i++) {
            Field f = allFields[i];
            int mods = f.getModifiers();
            if (Modifier.isPublic(mods) &&
                Modifier.isStatic(mods) &&
                Modifier.isFinal(mods)) {
                String name = f.getName();
                Object value = f.get(null);
                propMap.put(name, value);
            }
        }
        return Collections.unmodifiableMap(propMap);
    }

In JSP:

<c:set var="responseMap"

value="${applicationScope['javax.servlet.http.HttpServletResponse']}"/>
SC_NOT_FOUND: <c:out value="${responseMap.SC_NOT_FOUND}"/>


Search the archives on taglib-user for more details.


--
James Mitchell
Software Developer/Struts Evangelist
http://www.struts-atlanta.org
678-910-8017
AIM:jmitchtx


----- Original Message ----- 
From: "Mike Whittaker" <mi...@ntlworld.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>;
<al...@law.columbia.edu>
Sent: Friday, August 01, 2003 7:46 AM
Subject: RE: simple - import


>
> >
> ><c:set var="yourVar"><%= Const.MY_CONST %></c:set>
> >
>
> Good grief is this the only way?
>
> a/ isn't the aim to do without scriptlets/expressions?
>
> b/ all the data exists, server side in the app, why do I have to jump
> through hoops, copying CONSTANTS over to another variable???
>
> :-(
>
> --
> Mike W
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


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


RE: simple - import

Posted by Mike Whittaker <mi...@ntlworld.com>.
>
><c:set var="yourVar"><%= Const.MY_CONST %></c:set>
>

Good grief is this the only way?

a/ isn't the aim to do without scriptlets/expressions?

b/ all the data exists, server side in the app, why do I have to jump
through hoops, copying CONSTANTS over to another variable???

:-(

--
Mike W


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


RE: simple - import

Posted by Alex Shneyderman <al...@law.columbia.edu>.
<c:set var="yourVar"><%= Const.MY_CONST %></c:set>

> -----Original Message-----
> From: Mike Whittaker [mailto:mike_whittaker@ntlworld.com]
> Sent: Friday, August 01, 2003 7:33 AM
> To: Struts List
> Subject: simple - import
> 
> 
> How do you get Constants (or anything none scoped ie app/sess/req)
into a
> jsp for use in EL
> 
> I can do
> 
> <%@ page import="com.blah.Const" %>
> 
> 
> 
> but not
> 
> > 
> This probably demonstrates a fundamental none understanding!
> 
> TIA
> --
> Mike W
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org



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