在使用JSP时,如何使用Struts 2 ModelDriven接口访问POJO中的属性? [英] How to access properties in the POJO using Struts 2 ModelDriven interface when you are using JSP?

查看:87
本文介绍了在使用JSP时,如何使用Struts 2 ModelDriven接口访问POJO中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现ModelDriven接口的动作类。这个ModelDriven是一个普通的POJO,问题是它的一个属性是另一个对象。

I have an action class that implements ModelDriven interface. This ModelDriven is a regular POJO, the problem is that one of its properties is another object.

想象一下,我的ModelDriven是一个名为Person的对象,我的人有一个属性叫做地址是另一个对象。 Address有常规属性,如String,Long等。

Imagine that my ModelDriven is a object called Person and my person has an attribute called Address that is another object. Address has regular properties such as String, Long and etc.

在我提交表单的JSP中,使用了所有常用属性,如String,int,long in Person是否正确映射,但所有应映射到地址的数据都不是。

In the JSP when I submit the form, all the regular properties used such as String, int, long in Person are mapped correctly, but all the data that should be mapped to address are not.

<s:textfield name="name" id="name" size="25" maxlength="15" />
<s:textfield name="address.zipcode" id="zipcode" size="25" maxlength="15" />

这就是我尝试映射属性的方法。名称属性我可以把它弄好,但是当涉及到人物地址中的属性时,这种方法不起作用。

That's how I try mapping the properties. The name property I can get it right, but when it comes to map the properties in the person's address this approach does not work.

我做错了什么?

及时,我的Address属性在Person中声明实例化对象,所以它永远不会为空。

In time, my Address property is declared in Person instantiating the object, so it's never null.

编辑:根据要求,这里是行动来源和DTO:

As requested, here the action source and the DTOs:

行动:

@Controller
@Scope("request")
public class AnAction extends BaseAction implements ModelDriven<FakeDTO> {

    private static final long serialVersionUID = 8238033889271514835L;

    @Autowired
    private FakeFacade facade;

    private FakeDTO fakeDTO = new FakeDTO();

    public String action01() {
        return Action.SUCCESS;
    }

    public String action02() {
        this.fakeDTO.setAnswer(this.fakeFacade.fakeFacadeMethod(this.fakeDTO.getComplexObject()));
        return Action.SUCCESS;
    }

    @Override
    public FakeDTO getModel() {
        return this.fakeDTO;
    }
}

主DTO:

public class FakeDTO implements BaseDTO {

    private static final long serialVersionUID = -2093038083351846003L;

    private FakeFilterDTO filter = new FakeFilterDTO();
    private String name;

    public FakeDTO() {
        super();
    }

    @Override
    public FakeFilterDTO getFilter() {
        return this.filter;
    }

    public void setFilter(final FakeFilterDTO filterParam) {
        this.filter = filterParam;
    }

    public String getName() {
        return this.name;
    }

    public String setName(final String nameParam) {
        this.name = nameParam;
    }
}

FakeFilterDTO:

The FakeFilterDTO:

public class FakeFilterDTO extends BaseFilterDTO {

    private static final long serialVersionUID = 4528040257605851210L;

    private Date aDate;
    private Long aLong;
    private Integer anInteger;
    private String aString;

    public Date getADate() {
        return this.aDate;
    }

    public void setDataInicial(final Date aDateParam) {
        this.aDate = aDateParam;
    }

    public Long getALong() {
        return this.aLong;
    }

    public void setALong(final Long aLongParam) {
        this.aLong = aLongParam;
    }

    public Integer getAnInteger() {
        return this.anInteger;
    }

    public void setAnInteger(final Integer anIntegerParam) {
        this.anInteger = anIntegerParam;
    }

    public String getAString() {
        return this.aString;
    }

    public void setAString(final String aStringParam) {
        this.aString = aStringParam;
    }
}

struts.xml

The struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
    <struts>

    <include file="META-INF/bsad/struts2/struts-config.xml" />

    <package name="reports" namespace="/reports" extends="project-default">
        <action name="anAction" class="anAction" method="action01">
            <result>/WEB-INF/pages/success.jsp</result>
            <result name="input">/WEB-INF/pages/input.jsp</result>
        </action>

    <action name="generateReport" class="anAction" method="action02">
            <result>/WEB-INF/pages/reportGenerated.jsp</result>
        </action>
    </package>
</struts>

项目默认位于include struts-config.xml中并扩展struts-default包包含ModelDrivenInterceptor。我可以保证,因为我在这个拦截器中放置了一个断点并且它在那里传递。

The project-default is placed in the include struts-config.xml and extends struts-default package that contains the ModelDrivenInterceptor. I can assure that because I placed a break point in this interceptor and its passing through there.

我之前用作示例的JSP将如下所示:

The JSP that I used as an example before would become as follows:

<s:textfield name="name" id="name" size="25" maxlength="15" />
<s:textfield name="filter.aString" id="zipcode" size="25" maxlength="15" />

对于公司政策,我不允许复制/粘贴实际的对象及其名称。但这就是主意。

For company policies I'm not allowed to copy/paste the actual Objects and its names. But that's the idea.

推荐答案

fakeDTO 这是你的模特你应该有一个属性地址哪个应该返回一个像 AddressDTO 这样的对象,在这个对象中应该有一个属性 zipcode

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;

  }
 ...
}

因为你还没有发布 struts.xml 你的动作配置应包括 modelDriven 拦截器,其中包含默认情况下,在扩展 struts-default 包时使用defaultStack 。请参阅模型驱动的示例。该模型被拦截器推送到 valueStack 顶部,因此对象如地址应该可用,如果它有默认构造函数它将由OGNL创建,并在那里设置 zipcode 。当您在JSP中显示字段时, address.zipcode 将被评估为OGNL表达式并从地址中检索 zipcode bean如果模型初始化了bean和zipcode本身。 OGNL表达式中引用的所有bean都应该初始化并具有getter / setter属性。

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.

这篇关于在使用JSP时,如何使用Struts 2 ModelDriven接口访问POJO中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆