Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I want to use setter, getter and is together  RSS feed

 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i want to use getter, setter and is for one attribute together in one class just i given below, Is it possible...

public boolean isLength(){
return true;
}

public void setLength(String length) {
this.m_length = length;
}

public String getLength() {
return m_length;
}

Beacause i got the error like that : "Unable to find setter method for attribute : length

Why that happens.


Regards,
Indravadan Patel
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the name of your property , is it length or m_length cause you are appearing to set the value of m_length.
and secondly why do you need the isLength() method
It appeares from your code that the name of our property is m_length and not length
youe method reads m_length = length so you are setting the value of m_length
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as long as the getter and setter have the same name it doesn't matter that the attribute is called m_length.

I advise against supplying isLength() for something that is not a boolean value. It is misleading and may lead to errors. We often have getValue and isValue when it is a boolean, but otherwise not.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unable to find setter method for attribute : length

Where did you get that error ?
 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
as long as the getter and setter have the same name it doesn't matter that the attribute is called m_length.

I advise against supplying isLength() for something that is not a boolean value. It is misleading and may lead to errors. We often have getValue and isValue when it is a boolean, but otherwise not.


Hi,

I have use length in JSP as attribute and set, get and is methos in Tag handler classs. i have defined the length attribute in tld file.

Regards and thanks,
Indravadan
 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i here show you my JSP, TLD and Taghandler class.
----------------------------------
<%@ taglib uri="/WEB-INF/tlds/SimpleTag.tld" prefix="csajsp" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<csajsp:Multi length="20"/><br>
<csajsp:Multi length="40"/>

</body>
</html>
----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>simpletag</short-name>
<uri>/WEB-INF/tlds/SimpleTag</uri>
<tag>
<name>Multi</name>
<tag-class>indra.SimplePrimeTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>length</name>
<required>true</required>
</attribute>
</tag>
</taglib>
------------------------------------------------
package indra;

import java.math.BigInteger;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import java.io.*;
import java.math.*;
public class SimplePrimeTag extends TagSupport {
private String length="60";
// private boolean length;

public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
int len = Integer.parseInt(getLength());
out.println("Multiplication :" +len*len);
} catch (Exception ex) {

}

return (SKIP_BODY);
}


public boolean isLength(){
return true;
}

public void setLength(String length) {
this.length = length;
}

public String getLength() {
return length;
}



}
---------------------------------
and i got error like that :

org.apache.jasper.JasperException: /index.jsp(11,12) Unable to find setter method for attribute: length
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

-----------------------------

Regards,
Indravadan Patel
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better to use the same uri in the tld :

and in the jsp:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here comes from the isLength method. It is causing confusion as it looks like a getter method for the length attribute. You should rename it to something else.
 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christophe Verre:
The problem here comes from the isLength method. It is causing confusion as it looks like a getter method for the length attribute. You should rename it to something else.


Hi i know that the problem with isLength but i want to put that one in my code and i want to resolve that problem

Thanks,
Indravadan Patel
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you want to try removing it to see if the code works, then maybe we can start from a point that works rather than trying to guess what the issue is?

Please?
 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
Did you want to try removing it to see if the code works, then maybe we can start from a point that works rather than trying to guess what the issue is?

Please?



Hi,

I have remove that isLength() from code and it was working fine.

Regards,
Indravadan patel
 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
Did you want to try removing it to see if the code works, then maybe we can start from a point that works rather than trying to guess what the issue is?

Please?


Hi,

I have removed the isLength() method from the code and at that time it is working fine. If i put the setLength(boolean length) with isLength(), at that time the String setLength and getLength methods are not working menas that i can not get error but the result is not as per expectation.

Regards,
Indravadan Patel
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to call it something else. The server is using reflection to match the values with getters and setters on the object. If you do not set up your object according to the JavaBean specification, then your code is likely to be unpredictable and wrong.

Short answer: if it doesn't work, don't do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!