2016-06-10 28 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