如何以struts形式检索关联的属性

时间:2016-06-10 03:47:57

标签: java jsp struts-1

我试图熟悉Struts 1,因为它仍然在我们当前的项目中使用。创建一个简单的数据输入应用程序后,我遇到了一个例外情况;

  

javax.servlet.jsp.JspException:getter抛出的异常   物业:" city" bean:" studentForm"

  1. Struts HTML表单是否有办法访问Address字段;市和省?
  2. Struts HTML表单属性标记是否只接受字符串数据类型?
  3. 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;
        }
    

    地址类

    @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>
    

1 个答案:

答案 0 :(得分:0)

  
      
  1. Struts HTML表单是否有办法访问Address字段;市和省?
  2.   

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>
  
      
  1. Struts HTML表单属性标记是否只接受字符串数据类型?
  2.   

您可以在ActionForm中指定其他类型,转换可以自定义。对于Struts1,请阅读this FAQ 对于Struts2,请参阅Type Conversion