gpt4 book ai didi

java - struts2中如何使用表单提交复合对象

转载 作者:行者123 更新时间:2023-12-01 14:48:35 26 4
gpt4 key购买 nike

我有以下类(class)

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 {
...
}

例如,我可以使用以下代码访问地址类的单元字段

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

但是当我提交表单时,它会发送除地址类字段之外的所有字段。显示单位为空。

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

最佳答案

尝试

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

关于java - struts2中如何使用表单提交复合对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com