• Post Reply Bookmark Topic Watch Topic
  • New Topic

Attributes in JSP tags

 
john_guthrie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a tag set up now that looks something like this:
<package:mytag value1="howdy">
</package:mytag>
I'd like to be able to use a static string from another class to initialize the value1 attribute, but when I put something like:
<package:mytag value1=AnotherClass.HOWDY_STRING>
</package:mytag>
I get JSP compile errors. Anyone know if this is doable, and, if so, what the syntax is?
Thanks. John
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Jhon,
What about this ?
<package:mytag value1="<%= AnotherClass.HOWDY_STRING %>" >
</package:mytag>
 
john_guthrie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that way really confuses the JSP compiler (using weblogic 5.1). It may just be BEA's implementation, but it looks like they are trying to do a String.valueOf() whatever is in quotes, and that thing cannot have JSP code. And without the quotes the tag is not recognized at all (I get an error on the closing tag saying there is no corresponding opening tag).
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jhon,
can you tell me what does the value you actually want to assign to value1 ie inside your Taglib class setXXX method.
I do not think that is there any other way by which you can pass any attribute to your tag lib classes from the jsp page.All the atttributes will be passed as Strings and the container will automatically type cast them to the corresponding data type
 
john_guthrie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start, thanks for the helpful replies.
The attribute being set is a String in the Tag class. The static class variable I am trying to use is also a String. It looks to me like the JSP tag, as you say, expects the attribute initializer to be a String, but not only that, it wants a literal, surrounded by quotes. If this is not the case, or if it works differently in other app servers, I'd be interested in knowing.
Again, thanks.
 
Margaret Carver
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"john_guthrie",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp .
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I have seen dynamic values being done in tag attributes using <%= %>. What's more, is that it was not even String.
<%@ taglib uri="/hello" prefix="examples" %>
<%
java.util.List names = new java.util.LinkedList();
names.add("Rod");
names.add("Isabella");
names.add("Bob");
%>
<html>
<head>
<title>Name Tag</title>
</head>
<body>
HTML.
<p/>
<i>
<examples:hellos names="<%names%>">
Hello <%=name>. You're entry <%=index%> in my list.</br>
<examples:hellos/>

My name is <%=className%>.
The time is <%=date%><p/>
<i>
More HTML.
</body>
</html>
From Page 561 Professional Java Server Programming J2EE, Wrox.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!