Struts2问题 - 表单中的值未显示

时间:2011-02-28 03:53:57

标签: java jsp 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";
}

3 个答案:

答案 0 :(得分:0)

您是否在转发之前在动作类中设置了“公告”迭代器?

答案 1 :(得分:0)

以下代码未经过测试

public MyActionClass extends ActionSpport{
  public List<Bulletin> bulletins; //not encapsulated to shorten example, if you do add getters/setters the JSP will continue to work. 
  public String execute() {
    BulletinDAO bulletinDAOInstance = new BulletinDAO();
    bulletins = bulletinDAOInstance.getAllBulletins();
    if (bulletins != null) {
      return "success";
    }else{
      return "failure";
    }
}

Struts2将Action对象推送到值堆栈,因此您无需将其值推送到会话上以访问它。会话应该只保存长期存在的值。也许是用户偏好...如果您确实需要访问会话值,请使用#session.property,请参阅此处:http://struts.apache.org/2.0.11.1/docs/ognl.html

在提供的代码中存在一些小问题。如果使操作类实现Preparable接口,则可以移动逻辑以将DAO设置为prepare()。可以将公告测试移到validate(),但由于各种原因,它在execute()中可能更好。

答案 2 :(得分:0)

这是我用来最终获取隐藏标记中的id变量的代码。它并不需要太多。

<s:hidden name="id" value="%{id}" /></td>
相关问题