【问题标题】:How to retrieve an associated property in a struts form如何以struts形式检索关联的属性
【发布时间】:2016-06-10 03:47:56
【问题描述】:

我正在尝试熟悉 Struts 1,因为它仍在我们当前的项目中使用。在创建一个简单的数据输入应用程序时,我遇到了一个异常:

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

地址类

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

【问题讨论】:

    标签: java jsp struts-1


    【解决方案1】:
    1. Struts HTML 表单是否可以访问地址字段?城市和省份?

    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 表单属性标签是否只接受字符串数据类型?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-22
      • 2010-09-23
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多