Struts问题——s:hidden

发布于 2024-10-19 06:00:29 字数 2276 浏览 2 评论 0原文

我的 JSP 中有以下代码。我正在使用 Struts 表单,在其中传递要迭代的列表,并且列表中的每个项目都有其自己单独的表单。列表中的所有字段都是字符串,除了 id 之外,它是一个 int。我试图在 as:hidden 标记中呈现 int,但 Eclipse 告诉我“属性(值)无效”。 Struts 文档说该值采用字符串,但我将Bulletins.id 包装在${...} 内,这应该将其转换为字符串。我已经进行了多次谷歌搜索来尝试解决这个问题,但我一无所获。有人见过这个吗?

    <%@ page import="java.io.*"%>
    <%@ page import="java.util.List"%>
    <%@ page language="java" import="model.Bulletin"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>

    <!-- Some more code here that's not relevant to this problem -->

    <c:forEach var="bulletins" items="${bulletins}">
        <c:if test="${bulletins.approved == false}">
            <s:form action="ApproveBulletin" method="post">
                <table>
                    <tr>
                        <td colspan="2"><b>From:</b> <c:out value="${bulletins.name}" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><b>Subject:</b> <c:out value="${bulletins.subject}" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><b>Date:</b> <c:out value="${bulletins.date}" /> <br>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><c:out value="${bulletins.note}" />
<!-- Error here -->         <s:hidden name="id" value="${bulletins.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>
        </c:if>
    </c:forEach>

    <!-- Some more code here that's not relevant to this problem -->

I have the following code in my JSP. I'm using a Struts form where I'm passing in a List that I'm iterating over, and each item in the List has its own separate form. All fields in the list are Strings except for id, which is an int. I'm trying to render the int in a s:hidden tag, but Eclipse is telling me, "Invalid attribute (value)." The Struts documentation says that value takes a String, but I'm wrapping bulletins.id inside ${...}, which should convert it into a String. I've done multiple Google searches to try to resolve this issue, but I've come up with nothing. Has anyone seen this before?

    <%@ page import="java.io.*"%>
    <%@ page import="java.util.List"%>
    <%@ page language="java" import="model.Bulletin"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>

    <!-- Some more code here that's not relevant to this problem -->

    <c:forEach var="bulletins" items="${bulletins}">
        <c:if test="${bulletins.approved == false}">
            <s:form action="ApproveBulletin" method="post">
                <table>
                    <tr>
                        <td colspan="2"><b>From:</b> <c:out value="${bulletins.name}" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><b>Subject:</b> <c:out value="${bulletins.subject}" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><b>Date:</b> <c:out value="${bulletins.date}" /> <br>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><c:out value="${bulletins.note}" />
<!-- Error here -->         <s:hidden name="id" value="${bulletins.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>
        </c:if>
    </c:forEach>

    <!-- Some more code here that's not relevant to this problem -->

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

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

发布评论

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

评论(2

゛时过境迁 2024-10-26 06:00:29

Struts2 标签更喜欢 OGNL,因此将 ${} 替换为 %{} 甚至更好,只需写“

<s:hidden name="id" value="bulletins.id" />

这是个人偏好”,但我会只使用 S2 标签来编写该页面,如下所示:

<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" /></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>

几乎没有什么区别。请注意,迭代器堆栈将当前正在迭代的对象推送到堆栈上,因此您不需要迭代器元素内的点符号(对于更深层嵌套的对象,您将...)。

Struts2 tags prefer OGNL so replace ${} with %{} or even better just write

<s:hidden name="id" value="bulletins.id" />

This is personal preference but I'd write that page with only S2 tags as such:

<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" /></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>

There is very little difference. Note that the iterator stack pushes the current object being iterated onto the stack so you don't need dot notation inside the iterator element (well for more deeply nested objects you would...).

空名 2024-10-26 06:00:29

我对 Struts2 部分不太了解,但以下内容肯定是错误的:

<c:forEach var="bulletins" items="${bulletins}">

您正在用当前迭代的项目覆盖列表。用普通的 Java 术语来说,你基本上是在做:

for (Bulletin bulletins : bulletins) {}

这没有意义,也不会编译(重复的局部变量或类似的东西)。

给它一个不同且独特的名称。 bulletin 是最有意义的。循环内只有一个公告,对吗?

<c:forEach var="bulletin" items="${bulletins}">

然后将循环内的任何 ${bulletins.property} 替换为 ${bulletin.property}

I have no utter idea about the Struts2 part, but the following is definitely wrong:

<c:forEach var="bulletins" items="${bulletins}">

You're overwriting the list with the currently iterated item. In normal Java terms, you're basically doing:

for (Bulletin bulletins : bulletins) {}

This makes no sense and also wouldn't compile (duplicate local variable or something like that).

Give it a different and unique name. bulletin would make the most sense. There's means of only one bulletin inside the loop, right?

<c:forEach var="bulletin" items="${bulletins}">

and then replace any ${bulletins.property} inside the loop by ${bulletin.property}.

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