【发布时间】:2015-08-13 22:22:12
【问题描述】:
我这里的情况很奇怪。我已经在 JSF 中创建了我的应用程序,它看起来很棒,但是我不太确定以下内容:我想要一个超链接,以便一旦我单击我的书名,我就会进入一个包含有关该书的所有详细信息的页面。我的代码看起来到目前为止:
我的班级是这样的:
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;
}
}
我的控制器类如下所示:
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;
}
}
实体:
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;
}
}
我的 HTML 代码如下所示:
<?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>
请帮帮我,我不太确定如何从书籍列表中创建指向特定书籍的超链接(如代码所示)
谢谢。
附言
我的特定页面specificGame,它应该从listGames页面中点击的游戏中获取值。
<?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>
现在,当我单击页面列表游戏中游戏列表中的标题时,我将被重定向到特定的游戏页面,该页面应该如下所示:http://i.stack.imgur.com/OHmdp.png
由特定游戏的所有值填充。
【问题讨论】:
-
您的意思是类似于带有选定图书详细信息的弹出窗口或指向包含图书详细信息的新页面的链接?
-
感谢您的友好回复。我正在考虑重定向到另一个页面。我创建了一个名为 specificGame 的页面,现在一旦他单击列表中的某个标题,他就会被重定向到该页面并显示该特定游戏的所有值。我会用图像和那个类更新我的 Q.。
-
您可以通过将书籍 id 从视图传递给 backing-bean 来实现,搜索书籍,然后准备将其显示在目标页面中。看看这个部分:balusc.blogspot.fr/2011/09/…
-
我在阅读这篇文章时迷路了。我不太确定我会知道在我的应用程序中实现它。