Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Setting attributes in Tag files

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
<c:out value="${map.countryName}" />

But suppose, I wanted to retrieve in the following way:
<c:out 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christophe ... I got that correct!!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic