jsf 2 - Get to specific book using hyperlink in JSF -
this question has answer here:
i have strange situation here. have made application in jsf , looks great, not quite sure following: want have hyperlink once click on book title page details regarding book. code looks far:
my class looks 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 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 game g"), @namedquery(name = "game.findbytitle", query = "select g game g 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 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 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 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 main page</a> </h:body> </html>
please, me not quite sure how can make hyperlink specific book list of books( shown in code )
thanks.
p.s.
my specific page specificgame, supposed values game 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 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 main page</a> </h:body> </html>
now when click on title list of games on page list games redirected specific game page supposed this: http://i.stack.imgur.com/ohmdp.png
populated values particular game.
try change column title in listgames.xhtml :
<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 action method in backing bean :
public string searchgames(string tit) { sgamelist = gameejb.findgamesbytitle(tit); return "resultsgames.xhtml"; }
Comments
Post a Comment