JSP 到 Facelets 相当于数据库在循环中提供 URL 链接

发布于 2024-12-25 08:54:12 字数 1746 浏览 0 评论 0原文

我正在将 servlet/jsp Netbeans 的 Affableben 教程移植到 JSF 框架,并希望使用 Facelets 作为视图。

我已经有了 JPA 实体、会话 Bean 和托管 Bean。我从视图开始。但是,我还没有在 Facelets 中找到等效的方法来解决这一行:

<a href="<c:url value='category?${category.id}'/>">

这是完整的循环,无论是在 jsp 中还是在 Facelets 中:

JSP 代码:

<c:forEach var="category" items="${categories}">
    <div class="categoryBox">
        <a href="<c:url value='category?${category.id}'/>">
            <span class="categoryLabel"></span>
            <span class="categoryLabelText"><fmt:message key='${category.name}'/></span>

            <img src="${initParam.categoryImagePath}${category.name}.jpg"
                 alt="<fmt:message key='${category.name}'/>" class="categoryImage">
        </a>
    </div>
</c:forEach>

等效的 Facelets 代码:

<ui:repeat var="category" value="${categoryController.items}">
    <div class="categoryBox">
        <h:link outcome="${category.id}"/>
        <span class="categoryLabel"></span>
        <span class="categoryLabelText">${category.name}</span>

        <img src="./resources/img/categories/${category.name}.jpg"
                 alt="${category.name}" class="categoryImage"/>

    </div>
</ui:repeat>

此行在 Facelets 中无法按预期工作:

<h:link outcome="${category.id}"/>

What would be aworking Facelets 中的等价物?

编辑1

public String getName() throws UnsupportedEncodingException {
    return URLEncoder.encode(name, "UTF-8");
}

警告消息:无法找到资源img/categories/fruit+%26+veg.jpg

I'm porting the servlet/jsp Netbeans' Affableben tutorial to JSF Framework, and want to use Facelets for the view.

I already have the JPA entities, the session beans and the managed beans. I'm starting with the View. However, I have not found the equivalent in Facelets to work around this line:

<a href="<c:url value='category?${category.id}'/>">

This is the full loop, both in jsp and facelets:

JSP code:

<c:forEach var="category" items="${categories}">
    <div class="categoryBox">
        <a href="<c:url value='category?${category.id}'/>">
            <span class="categoryLabel"></span>
            <span class="categoryLabelText"><fmt:message key='${category.name}'/></span>

            <img src="${initParam.categoryImagePath}${category.name}.jpg"
                 alt="<fmt:message key='${category.name}'/>" class="categoryImage">
        </a>
    </div>
</c:forEach>

Equivalent Facelets code:

<ui:repeat var="category" value="${categoryController.items}">
    <div class="categoryBox">
        <h:link outcome="${category.id}"/>
        <span class="categoryLabel"></span>
        <span class="categoryLabelText">${category.name}</span>

        <img src="./resources/img/categories/${category.name}.jpg"
                 alt="${category.name}" class="categoryImage"/>

    </div>
</ui:repeat>

This line is not working in Facelets as expected:

<h:link outcome="${category.id}"/>

What would be a working equivalent in Facelets?

EDIT 1

public String getName() throws UnsupportedEncodingException {
    return URLEncoder.encode(name, "UTF-8");
}

Warning message: Unable to find resource img/categories/fruit+%26+veg.jpg

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

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

发布评论

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

评论(2

飘逸的'云 2025-01-01 08:54:12

利用 JSF 隐式导航并需要真实(隐式)导航结果值。您需要在结果中指定视图 ID。您需要通过指定请求参数。您还需要将跨度和图像嵌套在链接中,就像在最初的示例中所做的那样。假设您的根目录中有一个 category.xhtml 文件,则应该这样做:

<h:link outcome="category">
    <f:param name="id" value="#{category.id}" />

    <span class="categoryLabel"></span>
    <span class="categoryLabelText">#{category.name}</span>
    <img src="./resources/img/categories/#{category.name}.jpg"
             alt="#{category.name}" class="categoryImage"/>
</h:link>

与具体问题无关,您应该使用 也代替 。这样 JSF 将确保正确设置 src。在您的特定情况下,这将是

<h:graphicImage name="img/categories/#{category.name}.jpg"
    alt="#{category.name}" class="categoryImage"/>

(请注意,我将 ${} 替换为 #{},只是为了遵守标准并保持一致性)

The <h:link> utilizes JSF implicit navigation and requires a real (implicit) navigation outcome value. You need to specify the view ID in the outcome. You need to specify request parameters by <f:param>. You also need to nest the spans and the image in the link as you did in your initial example. Assuming that you have a category.xhtml file in the root, this should do:

<h:link outcome="category">
    <f:param name="id" value="#{category.id}" />

    <span class="categoryLabel"></span>
    <span class="categoryLabelText">#{category.name}</span>
    <img src="./resources/img/categories/#{category.name}.jpg"
             alt="#{category.name}" class="categoryImage"/>
</h:link>

Unrelated to the concrete question, you should be using <h:graphicImage> instead of <img> as well. This way JSF will ensure that the src is properly set. In your particular case that would be

<h:graphicImage name="img/categories/#{category.name}.jpg"
    alt="#{category.name}" class="categoryImage"/>

(note that I replaced ${} by #{}, just to adhere the standard and for the consistency)

零崎曲识 2025-01-01 08:54:12

您可以从这一行:更改

<a href="<c:url value='category?${category.id}'/>">

为:

<h:link outcome="category.xhtml?id=#{category.id}" />
<h:link outcome="category">
   <f:param name="id" value="#{category.id}" />
</h:link>
<h:outputLink value="category.xhtml?id=#{category.id}" ></h:outputLink>

You can change from this line:

<a href="<c:url value='category?${category.id}'/>">

to this:

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