• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting attributes in Tag files  RSS feed

 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Please find below the tag file and the corresponding jsp that invokes the tag.

Tag File:test.tag

<%@ attribute name="Country" required="true"%>
<%
java.util.Map map = new java.util.HashMap();
map.put("countryName", "India");
request.setAttribute("map", map);
%>

<c:set target="${map}" property="countryName" value="${Country}"/>


and the jsp snippet: TagFileTest.jsp


.......
<body>
<mytags:test Country="United States">
This is a test
</mytags:test>
</body>
....

whenever I try to invoke the above jsp page I always receive the following exception:

org.apache.jasper.JasperException: /TagFileTest.jsp(12,1) Unable to find setter method for attribute: Country.

Please let me know how I may set the value for Country to get around this exception.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried your code and saw the same thing.
Changing the attribute name from "Country" to "country" fixed it.

I'm not sure why.
I just browsed the JSP spec quickly and didn't see anything stating that attribute names need to follow bean naming conventions. It might be in there somewhere but, at a glance, I didn't see it.

To a Java developer, "Country" wouldn't be a good variable name so there is a good chance that most of us will never run into this issue.
[ March 29, 2008: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I'm not sure if you just forgot to include it or if it's really missing from your code but you don't have a tag declaration in your JSP.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your tag has no code to deal with the tag body so the text in your JSP isn't going to be shown.
Maybe you meant

to be
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ben. This got fixed when changed from Country to country.

I tried to retrieve the attribute in the tag file in the following way:

<c:set target="${map}" property="countryName" value="${country}" />
<br />
Country Name is
<cut value="${map.countryName}" />

But suppose, I wanted to retrieve in the following way:
<cut value="${map["countryName"]}"

When I try the same I receive the following error:

org.apache.jasper.JasperException: /WEB-INF/tags/test.tag(19,33) equal symbol expected

Please let me know how to get around this.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use double quotations inside double quotations like this. You need to either escape them, or use single quotations for the attribute:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"k_sam", please check your private messages for an important administrative matter.
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe ... I got that correct!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!