Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to access Constant from a constant class in JSTL(JSP page)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am again facing a problem as usual.Please somebody help me with that.
I have created a Constants.java to save constants and refer them where ever needed .I can access the constant variable in my action class in struts.
but I need to access it in my JSP page also where I am using JSTL.
I am presently using following code to access it...but its not working
<c:set var="customerDetailedInfoVo" value="${requestScope.Constants.CUST_INFO}"/>

---------------------------------------------
and my constants file is :-

public class Constants {

public static final String CUST_INFO = "custinfo";

}


Is there any other tag or way to access this in JSTL?

Thanks & Regards
Anisha
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aihadus tisidns:
Is there any other tag or way to access this in JSTL?



I never do this, but give it a try ,

Make the class of constant as Bean, i.e Have getter and setter method for all the constants field..

like,



Then access it using JSTL.
 
aihadus tisidns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried doing that also but it is still not working
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the error you are getting , when you implement the above case ?

OR If you are using Struts, why don't you try .properties files which contain all the constants you are using !!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you put the class into the request scope?
 
aihadus tisidns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I need to keep constants class in request scope?
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, without a scope how can a page will access it !!
 
aihadus tisidns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I am doing in my jSP code
---------------------------------------------

<%@page import="com.bbtnet.fd.custinfo.common.Constants;"%>

<c:set var="customerDetailedInfoVo" value="${requestScope.Constants.CUST_INFO}"/>

---------------------------------------------
and my constants file is :-

public class Constants {

public static final String CUST_INFO = "custinfo";

}

and in my action class:-
-------------------------------------------------
getServletRequest().setAttribute(Constants.CUST_INFO, customerInfoBuisness.fetchCustomerDetails());
---------------------------------------------------
I am setting this constant in request object in able to retrieve it in action class successfully.
But I want to import the constant class in jsp and use it constant.
Above is the way how i am setting it in request scope.Is that implementation wrong?.........It is wrong as I am not able to fetch data from that code..so what should I use intead of this tag in JSTL:-
<c:set var="customerDetailedInfoVo" value="${requestScope.Constants.CUST_INFO}"/>
 
aihadus tisidns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No error is there..............only data fields are blank as this tag cannot fetch data from DAO layer may be way of fetching is wrong
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aihadus tisidns:
way of fetching is wrong



Yes, try value="<%= Constants.CUST_INFO %>" instead of EL
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're doing this:



and, for the sake of argument, say CUST_INFO='custInfo', then you'd access it through JSTL like this:



(Or whatever the JSTL syntax for getting request-scoped stuff is, I don't recall.)

This isn't a Struts question, though, this is plain ol' servlets/JEE.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scriptlet is pedestrian. Make sure that you are using jsp 2.x and add a taglib with a function like the one below. It will get any object type, not just strings, and could be modified to include dot notation for any constants class. You can make a custom functions TLD once and reuse it for every project you ever do in the future. As a final note, if this constants class is for actual user viewable content, not keys and the like, use a localizable resource file and jstls fmt:message instead.

/**
* usage: ${yourprefix:constant('constantName')}
*/
public static Object getConstant(String constantName)
{
try
{
Field field = Constants.class.getDeclaredField(constantName);
return field.get(null);
}
catch (Exception e)
{
return null;
}
}

in your tld:
<function>
<name>constant</name>
<function-class>gov.fema.web.nimcast.tools.taglib.Functions</function-class>
<function-signature>Object getConstant(java.lang.String)</function-signature>
</function>
 
Peter DeWeese
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, my method's only benefit over David Newton's is not needing to put the constants class into scope.
 
reply
    Bookmark Topic Watch Topic
  • New Topic