javastrutsstruts-1html-inputstruts-html

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.


Solution

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