简体   繁体   中英

JSF2.0 Beginner trouble with parameters and properties from bean

This evening I have started to play about with JSF 2.0 for the first time and I have managed to cobble together a managed bean which returns a list of films, and a xhtml page that is able to display a table of the films that are returned from the managed bean.

After moving on, I attempted to put a link into the datatable with a view of creating a separate xhtml page that will just show details of a single film. But I seem to be having trouble working with the film bean in the new xhtml page.

Here is the code from the main listing xhtml page:

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

And here is the code from the managed bean that backs this page:

@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
    }
}

Now the idea I am attempting to recreate is when a user presses the link in the table found in the main listing xhtml page, they are redirected to a new xhtml page, which should get the Film that should be displayed via the getFilm(String id) method in the managed bean.

Here is simply what I have at the moment, which only prints out the basic toString() of the film object, for example: 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>

What I want to simply show here for starters is the title of the film, and I have tried different combinations of #{filmResourceBean.getFilm(id).title} , or #{filmResourceBean.getFilm(id)}.title , etc but none of them work, and being new to JSF, I'm not really sure where to look or what I am looking for. The tutorials I have found on the net don't show me a way to do this either, or at least not what I have managed to spot. Can someone point me into the right direction?

You where almost correct, you need to use #{filmResourceBean.getFilm(id).title} but your problem is with the parameter id . When openning a view using f:param it will pass the parameter as a GET parameter. To use it in the destination view, you need to use param['key'] so in your case the final code should be :

#{filmResourceBean.getFilm(param['id']).title}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM