0

I have a very strange situation here. I have made my application in JSF and it looks just great, however I am not quite sure about following: I want to have a hyperlink so that once I click on my book title I get to a page with all the details regarding that book. My code looks so far:

My class looks like this:

    package com.century.rental;

    import javax.ejb.Stateless;
    import javax.persistence.*;
    import java.util.List;

    @Stateless
    public class GameEJB {

        @PersistenceContext(unitName = "PerUni")
        private EntityManager em;

        public List<Game> findGames() {
            TypedQuery<Game> query = em.createNamedQuery("Game.findAll", Game.class);
            return query.getResultList();
        }

        public List<Game> findGamesByTitle(String title) {
            TypedQuery<Game> query = em.createNamedQuery("Game.findByTitle", Game.class);
            query.setParameter("title", title);
            return query.getResultList();
        }

        public Game find(Long id) {
            return em.find(Game.class, id);
        }

        public Game createGame(Game game) {
            em.persist(game);
            System.out.print("game stored");
            return game;
        }
    }

My controller class looks like this:

    package com.century.rental;

    import java.util.ArrayList;
    import java.util.List;
    import javax.ejb.EJB;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;

    @ManagedBean
    @SessionScoped
    public class GameController {

        @EJB
        private GameEJB gameEJB;

        private Game game = new Game();
        private String title = new String();
        private List<Game> gameList = new ArrayList<Game>();
        private List<Game> sgameList = new ArrayList<Game>();

        public GameController() {
        }

        public String doCreateGame() {
            gameEJB.createGame(game);
            gameList = gameEJB.findGames();
            game = new Game();
            return "listGames.xhtml";
        }

        public Game getGame() {
            return this.game;
        }

        public void setGame(Game game) {
            this.game = game;
        }

        public List<Game> getGameList() {
            gameList = gameEJB.findGames();
            return gameList;
        }

        public void setGameList(List<Game> gameList) {
            this.gameList = gameList;
        }

        public String searchGames() {
            sgameList = gameEJB.findGamesByTitle(title);
            return "resultsGames.xhtml";
        }

        public List<Game> getSgameList() {
            return sgameList;
        }

        public void setSbookList(List<Game> sgameList) {
            this.sgameList = sgameList;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

    }

Entity:

package com.century.rental;

    import java.util.Date;
    import javax.persistence.*;

    @Entity
    @NamedQueries({
        @NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
        @NamedQuery(name = "Game.findByTitle", query = "SELECT g FROM Game g WHERE g.title = :title")
    })

    public class Game extends Product {

        @Basic(optional = false)
        @Column(name = "DEVELOPER_STUDIO", nullable = false, length = 100)
        private String developerStudio;

        @Basic(optional = false)
        @Column(name = "PLATFORM", nullable = false, length = 100)
        private String platform;

        public Game() {

        }

        public Game(String title, String description, String rating, Date releaseDate, String developerStudio, String platform) {
            super(title, description, rating, releaseDate);
            this.developerStudio = developerStudio;
            this.platform = platform;
        }

        public String getDeveloperStudio() {
            return developerStudio;
        }

        public void setDeveloperStudio(String developerStudio) {
            this.developerStudio = developerStudio;
        }

        public String getPlatform() {
            return platform;
        }

        public void setPlatform(String platform) {
            this.platform = platform;
        }

    }

My HTML code looks like this:

    <?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>List of All Available Games in Database</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h1><h:outputText value="List of All Available Games in Database"/></h1>
                <h:dataTable value="#{gameController.gameList}" var="item" border="1">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Id"/>
                        </f:facet>
                        <h:outputText value="#{item.id}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Title"/>
                        </f:facet>
                        <h:outputText value="#{item.title}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Description"/>
                        </f:facet>
                        <h:outputText value="#{item.description}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Rating"/>
                        </f:facet>
                        <h:outputText value="#{item.rating}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Release Date"/>
                        </f:facet>
                        <h:outputText value="#{item.releaseDate}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Developer Studio"/>
                        </f:facet>
                        <h:outputText value="#{item.developerStudio}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Platform"/>
                        </f:facet>
                        <h:outputText value="#{item.platform}"/>
                    </h:column>
                </h:dataTable>
            </h:form>
        </f:view>
        <br /><br />
        <a href="newGame.faces">Add New</a> -OR- <a href="index.faces">Go to Main Page</a>
    </h:body>
</html>

Please, help me I am not quite sure how can I make a hyperlink to a specific book from a list of books( as shown in the code )

Thanks.

P.S.

My specific page specificGame, which is supposed to get values from a game that was clicked in listGames page.

    <?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>List specific Game in Database</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h1><h:outputText value="List a specific Game in Database"/></h1>
                <h:dataTable value="#{GameController.sGameList}" var="item" border="1">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Name"/>
                        </f:facet>
                        <h:outputText value="#{item.name}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Title"/>
                        </f:facet>
                        <h:outputText value="#{item.title}"/>
                    </h:column
                </h:dataTable>
            </h:form>
        </f:view>
        <br /><br />
        <a href="newGame.faces">Add New</a> -OR- <a href="index.faces">Go to Main Page</a>
    </h:body>
</html>

Now when I click on a title from the list of games on page list Games I would be redirected to specific Game page which is supposed to look like this: https://i.stack.imgur.com/OHmdp.png

populated by all the values from the particular game.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You mean something like a popup with selected book details oder link to a new page with book details? – pL4Gu33 May 31 '15 at 12:09
  • Thank you for your kind reply. I was thinking to redirect to another page. I have created a page called specificGame, now once he clicks on some title in a list he gets redirected to that page and displays all values of that particular game. I will update my Q. with images and that class. – SofraSofra May 31 '15 at 12:12
  • You can achieve that by passing the book id from the view to the backing-bean, search for the book, then prepare it for displaying in the desintation page. Take o look at this section : http://balusc.blogspot.fr/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters – Omar May 31 '15 at 12:19
  • I have lost a track reading this. I am not quite sure I will know to implement it in my application. – SofraSofra May 31 '15 at 12:25

1 Answers1

0

Try to change the column title in listGames.xhtml to :

<h:column>
   <f:facet name="header"> 
       <h:outputText value="Title"/>
   </f:facet>
   <h:commandLink value="#{item.title}" action="#{gameController.searchGames(item.title)}" />
</h:column>

Then, adapt the action method in the backing bean to :

public String searchGames(String tit) {
    sgameList = gameEJB.findGamesByTitle(tit);
    return "resultsGames.xhtml";
}
Omar
  • 1,430
  • 1
  • 14
  • 31