Struts2问题——表单中的值未显示
我刚刚在 JSP 中获得了有关以下 Struts2 表单的帮助。没有显示任何值。有人可以帮忙吗?
<s:iterator value="bulletins">
<s:if test="approved == false">
<s:form action="ApproveBulletin" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <s:property value="name" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <s:property value="subject" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <s:property value="date" /> <br>
</td>
</tr>
<tr>
<td colspan="2"><s:property value="note" />
<s:hidden name="id" value="id" /></td>
</tr>
<tr>
<td><s:submit type="button" value="approve" label="Approve"
action="ApproveBuletin" /></td>
<td><s:submit type="button" value="deny" label="Deny"
action="DenyBulletin" /></td>
</tr>
</table>
<br />
</s:form>
</s:if>
</s:iterator>
这是我的操作类中的代码,它将迭代器传递到 JSP。
public String execute() {
BulletinDAO bulletinDAOInstance = new BulletinDAO();
List<Bulletin> bulletins = bulletinDAOInstance.getAllBulletins();
if (bulletins != null) {
HttpSession session = (HttpSession) request.getSession();
session.setAttribute("bulletins", bulletins.iterator());
return "success";
}
return "failure";
}
I just got help with the following Struts2 form in my JSP. No values are being displayed. Can anyone help?
<s:iterator value="bulletins">
<s:if test="approved == false">
<s:form action="ApproveBulletin" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <s:property value="name" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <s:property value="subject" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <s:property value="date" /> <br>
</td>
</tr>
<tr>
<td colspan="2"><s:property value="note" />
<s:hidden name="id" value="id" /></td>
</tr>
<tr>
<td><s:submit type="button" value="approve" label="Approve"
action="ApproveBuletin" /></td>
<td><s:submit type="button" value="deny" label="Deny"
action="DenyBulletin" /></td>
</tr>
</table>
<br />
</s:form>
</s:if>
</s:iterator>
This is the code from my action class that passes my iterator to my JSP.
public String execute() {
BulletinDAO bulletinDAOInstance = new BulletinDAO();
List<Bulletin> bulletins = bulletinDAOInstance.getAllBulletins();
if (bulletins != null) {
HttpSession session = (HttpSession) request.getSession();
session.setAttribute("bulletins", bulletins.iterator());
return "success";
}
return "failure";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在转发之前在操作类中设置了“公告”迭代器?
Are you setting the "bulletins" iterator in your action class before the forward?
以下未经测试的代码
Struts2 将 Action 对象推送到值堆栈上,因此您不需要将其值推送到会话上来访问它。会话应该只保存长期存在的值。也许是用户偏好...如果您确实需要访问会话值,请使用
#session.property
请参阅此处:http://struts.apache.org/2.0.11.1/docs/ognl.html在提供的代码中存在一些小问题。如果使操作类实现 Preparable 接口,则可以将设置 DAO 的逻辑移至prepare() 中。可以将公告测试移至 validate(),但由于各种原因,将公告测试移至 validate() 可能会更好。
Following code not tested
Struts2 pushes the Action Object onto the value stack, as such you don't need to push it's value onto the session to access it. The session should only hold values that are long lived. Perhaps user preferences... If you really do need to access a session value use
#session.property
see here: http://struts.apache.org/2.0.11.1/docs/ognl.htmlIn the provided code there are some small issues. If you make the action class implement the Preparable interface, you can move the logic to set the DAO into prepare(). It's possible to move the test of bulletins to validate() but for various reasons it's probably better in execute() where it is.
这是我用来最终获取要在隐藏标记中呈现的 id 变量的代码。不需要太多。
Here is the code I used to finally get the id variable to render in the hidden tag. It didn't need much.