1

今天晚上我第一次开始使用 JSF 2.0,我设法拼凑了一个返回电影列表的托管 bean,以及一个能够显示从返回的电影表的 xhtml 页面托管 bean。

继续前进后,我尝试在数据表中添加一个链接,以创建一个单独的 xhtml 页面,该页面仅显示单个电影的详细信息。但我似乎无法在新的 xhtml 页面中使用电影 bean。

以下是主要清单 xhtml 页面中的代码:

<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
        <h:dataTable value="#{filmResourceBean.allFilms}" var="film">
            <h:column><f:facet name="header">Title</f:facet>
                <h:commandLink value="#{film.title}" action="film.xhtml" />
                <f:param name="id" value="#{film.imdbId}" />
            </h:column>
            <h:column><f:facet name="header">Plot</f:facet>#{film.plot}</h:column>
        </h:dataTable>
    </h:form>
</h:body>

以下是支持此页面的托管 bean 的代码:

@ManagedBean(name="filmResourceBean")
@SessionScoped
public class FilmResource implements Serializable {

    public List<Film> getAllFilms() {
    // This just returns a basic list of films, its not needed here to save space...
    }

    public Film getFilm(String id) {
    // THis just returns a film object that matches the given id, again to save space its not needed
    }
}

现在我试图重新创建的想法是,当用户按下在主列表 xhtml 页面中找到的表中的链接时,他们被重定向到一个新的 xhtml 页面,该页面应该获取应该通过 getFilm(String id ) 托管 bean 中的方法。

这里只是我目前所拥有的,它只打印出film对象的基本toString(),例如:org.jmcdonnell.mavenproject1.Film@34c3eb7c

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head >
    <h:body >
        <h:outputText value="#{filmResourceBean.getFilm(id)}"></h:outputText>
    </h:body>
</html>

我想在这里为初学者简单展示的是电影的标题,我尝试了#{filmResourceBean.getFilm(id).title}, 或#{filmResourceBean.getFilm(id)}.title等的不同组合,但它们都不起作用,而且作为 JSF 的新手,我不确定在哪里看或看什么我在寻找。我在网上找到的教程也没有告诉我这样做的方法,或者至少不是我设法发现的方法。有人可以指出我正确的方向吗?

4

1 回答 1

2

你几乎是正确的,你需要使用#{filmResourceBean.getFilm(id).title},但你的问题在于参数id。使用f:param它打开视图时,会将参数作为 GET 参数传递。要在目标视图中使用它,您需要使用param['key'],所以在您的情况下,最终代码应该是:

#{filmResourceBean.getFilm(param['id']).title}
于 2013-05-28T23:09:43.600 回答