| Author |
setting a bean in a JSP
|
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
I am having a problem with setting a beans parameters in a JSP page If I have say a variable String fname in a bean and I want to set it using the <jsp:setProperty i.e <jsp:setProperty name="connectorBean" property="fname" value="Tony"/> does this code call my setFname method void public setFname(String fname) { this.fname = fname; } or should it be void public setfname(String fname) { this.fname = fname; } or should fname be Fname. Very confused. I have gone back to using the setter and getter methods for know but would like to use the <jsp:setProperty and <jsp:getProperty tags Thanks for may help Tony
|
 |
Jeffrey Hunter
Ranch Hand
Joined: Apr 16, 2004
Posts: 305
|
|
Normally, your set and get methods should start with the respective "set" or "get", followed by the name of the member variable (starting with a capital). So, for instance, if your member variable is fname, your set method should be: public void setFname(String f) { fname = f; } Notice, you should not name your parameter (f), the same as the member variable (fname).
|
 |
Ali Gohar
Ranch Hand
Joined: Mar 18, 2004
Posts: 572
|
|
What is the error you getting? While making your bean class there should be a constructor with no parameters and setter,getter methods for every property. with set or get at the beginning and property name (with capital letter at 1st) after that.
|
 |
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
My bean is as follows: public class TestBean extends Object implements java.io.Serializable { private String fname; private String sname; private int age; public TestBean() { } public void setSname(String sname) { this.sname = sname; } public void setAge(int age) { this.age = age; } public String getFname() { return fname; } public String getSname() { return sname; } public int getAge() { return age; } } in my jsp I have the following: <jsp:useBean id="testbean" scope="session" class="revision.beanholder.TestBean" /> <jsp:setProperty name="testbean" property="fname" value="Tony" /> I get the error: Can't find a method to write property 'fname' of type 'java.lang.String' in a bean of type 'revision.beanholder.TestBean' I need a simple (very simple) tutorial that worls and show how to set parameters and get parameters from a bean using the jsp:get and set properties. Thanks for any help Tony
|
 |
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
Made a mistake for got to add the following methods public void setfname(String fname) { this.fname = fname; } public void setFname(String fname) { this.fname = fname; } still getting the same mistake: Thansk for any help Tony
|
 |
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
Got it working, seems that it was loading the error page, had to stop and restart Tomcat to get it working. Tony
|
 |
Jeffrey Hunter
Ranch Hand
Joined: Apr 16, 2004
Posts: 305
|
|
|
Keep in mind, when you make any changes to your Bean, you'll need to restart Tomcat (unless you configure Tomcat otherwise).
|
 |
 |
|
|
subject: setting a bean in a JSP
|
|
|