views:

56

answers:

3

Hi.

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="${UFForm.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.

A: 

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
I forgot to mention that it's already got these methods.
Bernhard V
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
If so, Getter and setter are essential for both the beans.
Dheeraj Joshi
I've got them for both. If I haven't got them, then I guess `<c:out value="${UFForm.company.address}" />` wouldn't work either.
Bernhard V
A: 

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.

dpb
Struts actually is doing `getCompany().getAddress()` and not `setAddress()`.
The Elite Gentleman
@The Elite Gentleman: You are correct! Somehow I associated the <html:input> 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.
dpb
A: 

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();
The Elite Gentleman
I can assign the `property` parameter twice in an `<html:text />` tag?
Bernhard V
@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 `<html:xxxx />` in Struts. (for a bean called `User`).
The Elite Gentleman
@Bernhard V, sorry...property cannot be declared twice....I've edited my solution. Thanks for spotting it. :-)
The Elite Gentleman