• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

setting a bean in a JSP

 
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep in mind, when you make any changes to your Bean, you'll need to restart Tomcat (unless you configure Tomcat otherwise).
 
I can't take it! You are too smart for me! Here is the tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic