jsp-tags


JSP custom tag library (Unable to find setter method for the attribute)


I'm having trouble with a custom tag:-
org.apache.jasper.JasperException: /custom_tags.jsp(1,0) Unable to find setter method for attribute : firstname
This is my TagHandler class:
package com.cg.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class NameTag extends TagSupport{
public String firstname;
public String lastname;
public void setFirstName(String firstname){
this.firstname=firstname;
}
public void setLastName(String lastname){
this.lastname=lastname;
}
public int doStartTag() throws JspException {
try {
JspWriter out=pageContext.getOut();
out.println( "First name: "+firstname+ "Last name: "+lastname);
} catch (Exception ex) {
throw new JspException("IO problems");
}
return SKIP_BODY;
}
}
This is my TLD file:
?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlibversion>1.1</tlibversion>
<jspversion>1.1</jspversion>
<shortname>utility</shortname>
<uri>/WEB-INF/nametagdesc.tld</uri>
<info>
A simple tag library for the examples
</info>
<tag>
<name>name</name>
<tagclass>com.cg.tags.NameTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>firstname</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>lastname</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
And this is my JSP page:
<%# taglib uri="/WEB-INF/nametagdesc.tld" prefix="cg" %>
<cg:name firstname="fname" lastname="lname"/>
I have checked that the code is recompiled and deployed correctly etc etc....
So, the question is , why can't it find the setter method???
Check the case of the attributes in your tag element - they should match the case of the setter, not the case of the member variables (Which should probably be private, by the way).
The rule is that the attribute name has its first letter capitalised and then the result is prefixed by 'set', to arrive at the setter name.
In your case, you've called the attribute 'firstname', so the rule results in the the JSP compiler looking for the 'setFirstname' method. As you've named your setter 'setFirstName' (with a capital 'N'), you should use 'firstName' (Also with a capital 'N') for the attribute name.
Apply the same rule to the 'lastname' attribute, to arrive at 'lastName', and you should be in business.
P.S. Using a good IDE, like IntelliJ, would have helped in this case, as it would have suggested the valid names for your attributes, saving you a lot of head scratching.
The TLD file in your example looks like nonsense, I don't know if it's because you've not formatted it correctly.
The tag element for your custom tag should have an attribute element that corresponds to each attribute you want to expose. Something like:
<tag>
<name>...</name>
<tag-class>...</tag-class>
<body-content>...</body-content>
<display-name>...</display-name>
<description>...</description>
<attribute>
<name>firstName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>...</description>
</attribute>
</tag>
Note that by default attributes are Strings. This can be overridden by adding a type element within the attribute element.

Related Links

creating and exporting a csv from Display tag table
bean write to number type id
restrict image file size in alloy UI
The function getContextPath must be used with a prefix when a default namespace is not specified
how can i retrive parameters from EncodeURL using jsp?
onclick tag giving error in h:commandLink
Displaytag - How to pass hidden value in url?
Embedding Tag Files in a JAR
Errors when the compiler is trying to build a custom JSP tag based on an Java Bean
Creating nested custom tag using jsp2 tag files
Unable to access session on JSP
Dealing with <html:options>
No tag “tableFacade” defined in tag library imported with prefix “jmesa”
how to refer values of one jsp file to other jsp
incorrect username and passwords
creating the welcome page

Categories

HOME
compiler-construction
arduino-uno
pypi
mfc
platform-builder
dot
mean-stack
jgroups
google-project-tango
contact
basic
facebook-php-sdk
ssl-client-authentication
tomcat6
modx-revolution
google-cloud-spanner
quickfix
hapi
google-static-maps
text-rendering
code-review
object-detection
sox
opennlp
vlsi
bootstrap-material-design
social-media
minitab
strncpy
nameservers
jspresso
google-qpx-express-api
vsts-build-task
phpfreechat
subset-sum
uiswipegesturerecognizer
gammu
revolution-slider
sqlite2
angularjs-factory
opshub
gulp-sourcemaps
g-code
fakeiteasy
import-from-excel
wptoolkit
komodoedit
rails-routing
celery-task
sage-one
dropbox-php
tropo
svmlight
nstextview
python-cryptography
django-scheduler
orthogonal
pintos
jquery-filter
google-feed-api
httplistener
infix-notation
database-optimization
underscore.js-templating
firebaseui
fputcsv
livequery
uid
wso2cloud
security-testing
0xdbe
ami
inmobi
csquery
website-monitoring
offloading
knuth
javafx-webengine
codeigniter-routing
qcodo
gulp-less
rebol3
fmod
pyhdf
oam
soundtouch
shellexecute
ocunit
path-separator
bluepill
newtonscript
dbproviderfactories
semantic-diff
asyncfileupload
punbb
work-stealing
nsdatecomponents
hirefire
cinema-4d
netbeans-7.1
locate
coda-slider
gallio
infrastructure
filtered-index
web-architecture
audio-capture
w3wp
dbisam
ugc
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App