开发者

Writing a string property of an object with <html:text />

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.


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();


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;
  }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜