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

发布于 2024-10-19 15:32:26 字数 1899 浏览 2 评论 0原文

我刚刚在 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 技术交流群。

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

发布评论

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

评论(3

甜妞爱困 2024-10-26 15:32:26

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

Are you setting the "bulletins" iterator in your action class before the forward?

穿透光 2024-10-26 15:32:26

以下未经测试的代码

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(),但由于各种原因,将公告测试移至 validate() 可能会更好。

Following code not tested

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 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.html

In 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.

寒冷纷飞旳雪 2024-10-26 15:32:26

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

<s:hidden name="id" value="%{id}" /></td>

Here is the code I used to finally get the id variable to render in the hidden tag. It didn't need much.

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