Struts2选择标签保存和召回问题

时间:2011-04-14 18:29:20

标签: java select struts2 jsp-tags

我在尝试使用struts2标记时感到困惑。我已经遵循了几个例子,他们似乎都有相同的使用模式,但这对我不起作用(下面是所有相关的代码)。

基本上,struts2选择下拉列表允许我显示初始列表并保存值,但是当我将页面抬起来编辑对象时,它无法正确显示。显示页面后,所有选择值都将显示默认值而不是预期值。如果我修改选择值并再次保存页面,它将正确保存该值。在某处,标记未正确设置选项列表的SELECTED值。我能够查看持久保存到数据库的正确更改,因此我知道该操作正确接收了该对象。

我认为问题出在JSP的<s:select list="lifeInsuranceStatuses" name="domainObject.lifeInsuranceCoverages[%{#status.index}].lifeInsuranceStatus.oid" listKey="oid" listValue="status" theme="simple" />行内,但我可能错了。看起来这个页面的所有部分都有效,除了在调用对象时设置下拉列表的正确值。

我期待这个

public class MemberAction
    extends AbstractAction<Member>
{
    private static final long serialVersionUID = 1L;
    private Service<LifeInsuranceStatus> lifeInsuranceStatusService;

    // Code removed for brevity...

    public List<LifeInsuranceStatus> getLifeInsuranceStatuses()
    {
        return lifeInsuranceStatusService.getObjects().getResponse();
    }

    public Service<LifeInsuranceStatus> getLifeInsuranceStatusService()
    {
        return lifeInsuranceStatusService;
    }

    public void setLifeInsuranceStatusService(Service<LifeInsuranceStatus> lifeInsuranceStatusService)
    {
        this.lifeInsuranceStatusService = lifeInsuranceStatusService;
    }
}

我的JSP页面如下所示:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="saveMember" method="post">
<table border="1">
    <tr>
        <td>
            <s:text name="domainObject.lifeInsuranceCoveragesn.lifeInsuranceStatus.status" />
        </td>
    </tr>
    <s:div id="LifeInsuranceCoverages" theme="simple">
        <s:iterator value="domainObject.lifeInsuranceCoverages" status="status">
            <tr class="lifeInsuranceCoverageRow<s:property value="%{#status.index}" />">
                <td>
                    <s:select list="lifeInsuranceStatuses" name="domainObject.lifeInsuranceCoverages[%{#status.index}].lifeInsuranceStatus.oid" listKey="oid" listValue="status" theme="simple" />
                </td>
            </tr>
        </s:iterator>
    </s:div>

    <tr id="addLifeInsuranceCoverageRow">
        <td>
            <a href="#" onClick="return LifeInsuranceCoverages.addRow()">Add Life Insurance Coverage...</a>
        </td>
    </tr>
</table>

<s:submit key="submit" />
</s:form>

我的模型看起来像是:

public class Member
    extends Person
{
    private static final long serialVersionUID = 1L;

    private List<LifeInsuranceCoverage> lifeInsuranceCoverages;

    public List<LifeInsuranceCoverage> getLifeInsuranceCoverages()
    {
        if (lifeInsuranceCoverages == null)
        {
            lifeInsuranceCoverages = new ArrayList<LifeInsuranceCoverage>();
        }

        return lifeInsuranceCoverages;
    }

    public LifeInsuranceCoverage getActiveLifeInsuranceCoverage()
    {
        if (lifeInsuranceCoverages == null || lifeInsuranceCoverages.size() < 1)
        {
            return null;
        }
        else
        {
            return lifeInsuranceCoverages.get(lifeInsuranceCoverages.size() - 1);
        }
    }

    public void setLifeInsuranceCoverages(List<LifeInsuranceCoverage> lifeInsuranceCoverages)
    {
        this.lifeInsuranceCoverages = lifeInsuranceCoverages;

        for (LifeInsuranceCoverage lifeInsuranceCoverage : this.lifeInsuranceCoverages)
        {
            lifeInsuranceCoverage.setMember(this);
        }
    }

    public void addLifeInsuranceCoverage(LifeInsuranceCoverage lifeInsuranceCoverage)
    {
        lifeInsuranceCoverage.setMember(this);
        getLifeInsuranceCoverages().add(lifeInsuranceCoverage);
    }

    public void removeLifeInsuranceCoverage(LifeInsuranceCoverage lifeInsuranceCoverage)
    {
        getLifeInsuranceCoverages().remove(lifeInsuranceCoverage);
        lifeInsuranceCoverage.setMember(null);
    }
}

......和......

public class LifeInsuranceCoverage
    extends AbstractDomainObject
{
    private static final long serialVersionUID = 1L;
    private Member member;
    private LifeInsuranceStatus lifeInsuranceStatus;

    public LifeInsuranceStatus getLifeInsuranceStatus()
    {
        return lifeInsuranceStatus;
    }

    public void setLifeInsuranceStatus(LifeInsuranceStatus lifeInsuranceStatus)
    {
        this.lifeInsuranceStatus = lifeInsuranceStatus;
    } 

    public Member getMember()
    {
        return member;
    }

    public void setMember(Member member)
    {
        this.member = member;
    }
}

......和......

public class LifeInsuranceStatus
    extends AbstractDomainObject
{
    private static final long serialVersionUID = 1L;
    private String status;

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }
}

......还有......

public abstract class AbstractDomainObject
    implements DomainObject
{
    private static final long serialVersionUID = 1L;
    private Long oid;

    public Long getOid()
    {
        return oid;
    }

    public void setOid(Long oid)
    {
        this.oid = oid;
    }
}

1 个答案:

答案 0 :(得分:0)

经过一些试验和错误,我能够弄清楚这个问题。我找到的解决方案对我没有意义,但无论如何都可以。

为了解决这个问题,我必须做两件事。首先,我在LifeInsuranceStatus类中添加了一个只返回status值的toString()方法。然后我删除了struts2标记的listKey部分。一旦我将这两件事放在一起,我存储在数据库中的值就会被正确调用并妥善保存。

我很想知道为什么这样做。任何人都对struts2标签有更好的了解,或者为什么另一个使用枚举的下拉工作完全正常而不删除标签的listKey部分......