简体   繁体   English

如何在Struts2中使用表单提交复合对象

[英]How to submit a composite object using form in struts2

I have the following classes 我有以下课程

public class Address {
  private String unit;
  private String street;
  ... getter and setters go here....
}

public class Person {
   private String name;
   private Address address;
   public Person(){
      address = new Address();
   }
   ... getter and setters go here...
 }

public class Employee extends Person {
    ... 
}

For example I can access the unit filed of address class using the following code 例如,我可以使用以下代码访问地址类的单位字段

 <s:form method="POST" action="updateEmployee">
    <s:textfield name ="unit" label="Unit" value="%{employee.address.unit}" />
    <s:textfield name ="name" label="Name" value="%{employee.name}" />
 </s:form>

but when I submit the form it sends all the fields except the fields of address class. 但是当我提交表单时,它会发送除地址类字段以外的所有字段。 It shows the unit is null. 它显示单位为空。

 @Action
 public class myaction implements ModelDriven {

 Employee emp = new Employee();

 public String updateEmployee(){
    System.out.println("Unit is" + employee.getAddress().getUnit();
    System.out.println("Unit is" + employee.getName();
    return "SUCCESS";
 }

 public Employee getEmployee() {
    return employee;
}

public void setEmployee(Employee employee) {
    this.employee = employee;
}

@Override
public Object getModel() {
    return employee;
}

Try 尝试

<s:textfield name ="employee.address.unit" label="Unit" value="%{employee.address.unit}" />
<s:textfield name ="employee.name" label="Name" value="%{employee.name}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM