Struts2选择标签保存和调用问题

发布于 2024-11-01 14:32:56 字数 5493 浏览 0 评论 0 原文

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

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

我相信问题出在 行,但我可能是错的。看起来这个页面的所有部分都有效,除了在调用对象时设置下拉菜单的正确值之外。

我期望这个

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

I am getting confused trying to use the struts2 tag. I have followed several examples and they all seem to have the same usage pattern, but this doesn't work for me (below is all of the code that is relevant).

Basically, the struts2 select drop down allows me to display the initial list and save the value, but does not get displayed correctly when I bring the page up to edit the object. Once the page is displayed, all select values display the default instead of the expected value. If I modify the select value and save the page again, it will correctly save the value. Somewhere, the tag is not properly setting the SELECTED value of the option list. I am able to view the correct change being persisted to the database, so I know the action is correctly receiving the object.

I believe the problem lies somewhere within the <s:select list="lifeInsuranceStatuses" name="domainObject.lifeInsuranceCoverages[%{#status.index}].lifeInsuranceStatus.oid" listKey="oid" listValue="status" theme="simple" /> line of the JSP, but I may be wrong. It seems like all of the pieces of this page work except for setting the proper value of the drop down when the object is recalled.

Where I expect this

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

My JSP page looks like the following:

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

And my model looks like:

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

... and ...

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

... and ...

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

... and also ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-11-08 14:32:56

经过一番尝试和错误后,我能够解决这个问题。我找到的解决方案对我来说没有意义,但无论如何它都有效。

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

我很想知道为什么会这样。任何人都对 struts2 标签有更好的了解,或者为什么另一个使用枚举的下拉菜单在不删除标签的 listKey 部分的情况下工作得很好......

I was able to figure out this issue after some trial and error. The solution I found doesn't make sense to me but it works regardless.

In order to fix this, I had to do two things. First, I added a toString() method in the LifeInsuranceStatus class that returned just the status value. Then I removed the listKey portion of the struts2 tag. Once I did these two things together, the value I stored in the database was recalled properly and saved properly.

I would love to know why this worked this way. Anyone have better knowledge of struts2 tags or why another drop down that used an enum worked perfectly fine without removing the listKey portion of the tag...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文