Struts2选择标签保存和调用问题
我在尝试使用 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番尝试和错误后,我能够解决这个问题。我找到的解决方案对我来说没有意义,但无论如何它都有效。
为了解决这个问题,我必须做两件事。首先,我在
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 thestatus
value. Then I removed thelistKey
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...