0

I've got an object in my form that contains various string properties.

When I want to print it in my JSP form I could do it with

<c:out value="${form.company.address}" />

which works perfectly.

Now I want to create an HTML input field. But when I write

<html:text property="company.address" />

I get an error saying

Caused by: javax.servlet.jsp.JspException: No getter method for property company.address of bean org.apache.struts.taglib.html.BEAN

Do you know how I can create an HTML input field with my company's address?

My bean's got the necessary corresponding getters and setters.

3 Answers3

2

The correct way of translating this:

<c:out value="${UFForm.company.address}" />

to Struts is,

<html:text name="UFForm" property="company.address">

It means that there's a request with name UFForm with a bean that contains a method getCompany() (which I'm assuming returns a Company object) and that in turns has a getAddress() getter (if you understand what I mean). In a nutshell, the bean from request/session UFForm, the TagLib is accessing getCompany().getAddress();

PS Hope that getAddress() doesn't return a null else <html:text /> will throw an exception.


Edit To explain what I did above:

public class Company implements Serializable {

    private String address;

    //Setter
    public void setAddress(String address) {
        this.address = address;
    }

    //Getter
    public String getAddress() { return this.address; }
}

public class UFForm implements Serializable {

    private Company company;

    public void setCompany(Company company) {
        this.company = company;
    }

    public void getCompany() {
        if (this.company == null) {
            setCompany(new Company());
        }

        return this.company;
    }
}

What I did above in <html:text /> is equivalent to

UFForm ufForm = ....;
String property = ufForm.getCompany().getAddress();
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I can assign the `property` parameter twice in an `` tag? –  Sep 17 '10 at 12:36
  • @Bernhard V, yes, if your parent class getter returns an object (not primitive). You can say also `user.address.postCode` inside a property field for any `` in Struts. (for a bean called `User`). – Buhake Sindi Sep 17 '10 at 13:19
  • @Bernhard V, sorry...property cannot be declared twice....I've edited my solution. Thanks for spotting it. :-) – Buhake Sindi Sep 17 '10 at 20:14
0

Your bean should have corresponding setter and getter methods.

Html form

<html:text property="name" size="10" maxlength="10">

Corresponding bean.

public class AddressForm 
{
  private String name=null;

  public void setName(String name){
    this.name=name;
  }

  public String getName(){
    return this.name;
  }
}
Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55
  • Do you have the getter/setter for the company? From your code it seems like there is a bean inside your Form named company who's address you are trying to print. – Faisal Feroz Sep 16 '10 at 08:32
  • If so, Getter and setter are essential for both the beans. – Dheeraj Joshi Sep 16 '10 at 08:41
  • I've got them for both. If I haven't got them, then I guess `` wouldn't work either. –  Sep 16 '10 at 09:10
0

When you are getting the value for the text box with:

<html:text property="company.address" />

You are in fact saying to Struts to do:

formObject.getCompany().getAddress();

So you must have a getter for the company (which must not return null or the next operation will fail) and a setter for the address on the company object. The setters/getters must be public. This must already be the case since you can do the following with no error:

<c:out value="${UFForm.company.address}" />

Now, the thing that bugs me is this part: ${UFForm.. When you use JSTL you are accessing the form explicitly. With the <html:text> you access a property on the form implicitly. This implicit form is provided by the enclosing <html:form> tag. Do you have the <html:text> inside a <html:form>?

The form bean is located/created/exposed based on the form bean specification for the associated ActionMapping so check your mapping also.

  • Struts actually is doing `getCompany().getAddress()` and not `setAddress()`. – Buhake Sindi Sep 16 '10 at 20:36
  • @The Elite Gentleman: You are correct! Somehow I associated the with setting the value and unconsciously used a setter. The form formObject.getCompany().getAddress() is used for getting the value and formObject.getCompany().setAddress(...) for setting it. I corrected my answer. Thanks for pointing that out. –  Sep 17 '10 at 20:36