2016-06-10 53 views
0

我正在嘗試熟悉Struts 1,因爲它仍在我們當前的項目中使用。在創建一個簡單的數據錄入應用程序時,我遇到了一個例外情況:如何檢索struts表單中的關聯屬性

javax.servlet.jsp.JspException:異常的getter方法 物業拋出:bean的 「城市」: 「studentForm」

  1. 是否有Struts的HTML表單訪問的方式地址字段;城市和省?
  2. Struts HTML表單屬性標籤是否只接受字符串數據類型?

Person.class

@MappedSuperclass 
public abstract class Person implements Serializable { 

    static SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy"); 

    @Column(name = "first_name", nullable = false, updatable = true, insertable = true) 
    private String firstName; 

    @Column(name = "last_name", nullable = false, updatable = true, insertable = true) 
    private String lastName; 

    @Column(name = "date_of_birth", nullable = false, updatable = true, insertable = true) 
    @Temporal(TemporalType.DATE) 
    private String dateOfBirth; 

    @Embedded 
    private Address address; 

public Address getAddress() { 
     return address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 

Address類

@Embeddable 
public class Address { 

    @Column(name = "city", nullable = false, updatable = true, insertable = true) 
    private String city; 

    @Column(name = "province", nullable = false, updatable = true, insertable = true) 
    private String province; 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city.toUpperCase(); 
    } 

    public String getProvince() { 
     return province; 
    } 

    public void setProvince(String province) { 
     this.province = province.toUpperCase(); 
    } 

助學行動形式

public class StudentForm extends ActionForm { 

    private StudentBean student = new StudentBean(); 
    private Address address = new Address(); 

public Address getAddress() { 
     return student.getAddress(); 
    } 

    public void setAddress(Address address) { 
     this.student.setAddress(address); 
    } 

    public String getCity() { 
     return student.getAddress().getCity(); 
    } 

    public void setCity(String city) { 
     this.student.getAddress().setCity(city); 
    } 

    public String getProvince() { 
     return student.getAddress().getProvince(); 
    } 

    public void setProvince(String province) { 
     this.student.getAddress().setProvince(province); 
    } 

的Struts 1 HTML表單

<html:form action="RegisterStudent.do"> 

     <label for="firstName">First Name: </label> 
     <html:text name="studentForm" property="firstName" /> 
     <br> 
     <label for="lastName">Last Name: </label> 
     <html:text name="studentForm" property="lastName" /> 
     <br> 
     <label for="dateOfBirth">Date of Birth(mm/dd/yyyy): </label> 
     <html:text name="studentForm" property="dateOfBirth" /> 
     <br> 
     <label for="city">City: </label> 
     <html:text name="studentForm" property="city" /> 
     <br> 
     <label for="province">Province: </label> 
     <html:text name="studentForm" property="province" /> 
     <br> 
     <label for="department">School Department: </label> 
     <html:text name="studentForm" property="department" /> 
     <br> 
     <html:submit>Register</html:submit> 

    </html:form> 

回答

0
  1. 有沒有一種方法讓Struts HTML表單訪問Address字段;城市和省?

的Struts可以填充請求參數的ActionForm嵌套對象。如果StudentBean延伸Person類。您可以修改頁面:

<html:form action="RegisterStudent.do"> 
    <label for="firstName">First Name: </label> 
    <html:text name="studentForm" property="student.firstName" /> 
    <br> 
    <label for="lastName">Last Name: </label> 
    <html:text name="studentForm" property="student.lastName" /> 
    <br> 
    <label for="dateOfBirth">Date of Birth(mm/dd/yyyy): </label> 
    <html:text name="studentForm" property="dateOfBirth" /> 
    <br> 
    <label for="city">City: </label> 
    <html:text name="studentForm" property="student.address.city" /> 
    <br> 
    <label for="province">Province: </label> 
    <html:text name="studentForm" property="student.address.province" /> 
    <br> 
    <label for="department">School Department: </label> 
    <html:text name="studentForm" property="department" /> 
    <br> 
    <html:submit>Register</html:submit> 
</html:form> 
  • 是否Struts的HTML表單屬性標籤只接受字符串數據類型?
  • 可以在ActionForm指定其他類型的轉換可以自定義。對於Struts1,請閱讀this FAQ
    對於Struts2,請參考Type Conversion