3

I want get java constants which declared in seperate java class techcontants.java, to be displayed in the jsp using jstl.

public class TechConstants {
   public static final String PART = "Part";
   public static final String DESCRIPTION = "Description";
   public static final String VERSION = "Version";
}

In my view.jsp i need this value like this using ustl

<td class="Thd" width="14%">Part</td>

(i have tried like this <c:outvalue="${TechConstants.PART}">)

<td class="Thd" width="20%">Description</td>
<td class="Thd" width="10%">Version</td>

I am using spring and jstl,

Do i need to get the constant value in my modelandview controller from there i have to pass it to view?

if so please describe me in what to do in controller with code example, or i can get constants directly in jsp using jstl?

AurA
  • 12,135
  • 7
  • 46
  • 63
anto
  • 315
  • 3
  • 9
  • 18
  • @Japan Trivedi Can you please help me – anto May 17 '12 at 05:39
  • is this a homework? if so please tag it. – raddykrish May 17 '12 at 06:22
  • http://stackoverflow.com/questions/127328/java-constants-in-jsp – raddykrish May 17 '12 at 06:27
  • @raddykrish hi krish im not using servlet for this case? i have tried created a get method for part public static String getPart() { return PART; } and tried accessing it like ${techconstant.Part} in jsp but i could get the values – anto May 17 '12 at 06:32
  • the tag library called unstandard taglib might be useful for you right. you need to download that and use in your jsp. if you are defining methods then it defeats the purpose of constants and will be come like java bean for which you might need servlets to return those beans to the jsp. – raddykrish May 17 '12 at 06:36
  • btw, you have tagged this with spring mvc right then you can have either maps or beans added as model attribute and bring that to the jsp pages and display that. – raddykrish May 17 '12 at 06:40
  • @raddykrish Thanks , i am a newbie can please explain the above by giving example code – anto May 17 '12 at 06:54
  • http://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el - you can see the usage of unstandard tag libs in the accepted answer. you need to download those jars and put in the classpath and import the tag lib in your jsp. – raddykrish May 17 '12 at 07:01
  • A duplicate of http://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el – AurA Jul 22 '13 at 09:10

2 Answers2

1

You can do it using JSP expression tag ,Please give a try with below code.

<%=TechConstants.PART %>
Alien
  • 15,141
  • 6
  • 37
  • 57
-2

You can access the properties of the object that you are binding to the form.

<form:form method="POST" commandName="user">

You can use ${user.name}