How to access properties in the POJO using Struts 2 ModelDriven interface when you are using JSP?

早过忘川 提交于 2019-12-01 00:49:20

In the fakeDTO that is your model you should have a property address which should return an object like AddressDTO in this object there should be a property zipcode.

public class FakeDTO implements BaseDTO {

  private AddressDTO address;

  public AddressDTO getAddress() {
    return address;
  }

  public void setAddress (AddressDTO address) {
    this.address = address;
  }
 ...
}

public class AddressDTO implements BaseDTO {

  private String zipcode;

  public String getZipcode() {
    return zipcode;
  }

  public void setZipcode(String zipcode) {
    this.zipcode = zipcode;

  }
 ...
}

as you haven't posted struts.xml your action configuration should include modelDriven interceptor which include in the defaultStack by default is used when you extend struts-default package. See example of model driven. The model is pushed to the top of the valueStack by the interceptor, so the object like address should be available if it has a default constructor it will be created by the OGNL and zipcode set there. When you display the fields in the JSP the address.zipcode is evaluated as an OGNL expression and retrieve zipcode from the address bean if the model is initialized that bean and zipcode itself. All beans referenced in OGNL expression should be initialized and have getter/setter properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!