所以我制作一个小型网络应用程序来获取有关游戏的信息,我已经制作了一个搜索到API的搜索栏,然后我获得了与搜索名称最相似的前10个游戏。但我只显示标题。
This is an example of a search
游戏有很多属性,其中一个是ID,它允许我使用控制器获取游戏的所有信息。
现在应用程序的工作方式如下:
1:你搜索游戏的标题
2.一个控制器给我一个ID列表和id,我得到关于游戏的其余信息,但我只显示标题。
现在,我想做什么:
3.当你点击一个标题时,你使用一个控制器,并从它请求的ID(再次,我不确定我是否可以使用以前的游戏对象)所有来自该游戏的信息并显示它。
控制器工作正常,但我遇到了HTML问题。
这是搜索的控制器:
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getParameter("searchQuery");
RequestDispatcher rd = null;
log.log(Level.FINE, "Searching for games that contain " + query);
GameResource game = new GameResource();
GameSearch[] gameResults = null;
gameResults = game.getGameSearch(query);
//System.out.println(Arrays.toString(gameResults));
List<Game> listaBusqueda = new ArrayList<Game>();
if (gameResults.length!=0){
rd = request.getRequestDispatcher("/success.jsp");
for (GameSearch g : gameResults) {
listaBusqueda.add(game.getGame(g.getId().toString()));
}
request.setAttribute("games", listaBusqueda);
} else {
log.log(Level.SEVERE, "Game object: " + gameResults);
rd = request.getRequestDispatcher("/error.jsp");
}
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
...
这是使用ID
给我一个游戏的控制器...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String gameId = request.getParameter("id");
GameResource resource = new GameResource();
RequestDispatcher rd = null;
log.log(Level.FINE, "Retrieving game");
Game game = resource.getGame(gameId);
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
它们都有效,在最后一个中正确创建了对象并获得了ID。
现在,这是success.jsp的html,这就是问题所在:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Search results</title>
</head>
<body>
<fieldset id="IGDB">
<legend>
IGDB search for
<c:out value="${param.searchQuery}" />
</legend>
<c:forEach items="${requestScope.games}" var="game">
<article name="gameArticle" id="gameArticle"></article>
<a href="GameIdController?id=${game.id}"><c:out value="${game.name}"
/></a>
<br />
</c:forEach>
</fieldset>
<c:if test="${requestScope.Item=='gameID' }">
<c:forEach items="${requestScope.Game}" var="game">
<article name="gameArticle" id="gameArticle"></article>
<c:out value="${game.name}" />
<br />
</c:forEach>
$game = ${requestScope.Game}
<c:out value="${game.name}"/>
</c:if>
<article name = "gameByIDArticle" id = "gameByIDArticle">
</article>
</body>
</html>
当我点击标题时,我会被重定向到一个空白网页(http://localhost:8090/GameIdController?id=IdOfTheGame),as,您可以看到该ID包含在网址中,但它完全空白,应该再次包含标题,如我所述代码。
我还将包含我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<description></description>
<display-name>GameSearchController</display-name>
<servlet-name>GameSearchController</servlet-name>
<servlet-class>aiss.controller.GameSearchController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GameSearchController</servlet-name>
<url-pattern>/GameSearchController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>GamePopularityController</display-name>
<servlet-name>GamePopularityController</servlet-name>
<servlet-class>aiss.controller.GamePopularityController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GamePopularityController</servlet-name>
<url-pattern>/GamePopularityController</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-
class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-
class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>aiss.api.GameApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>GameIdController</display-name>
<servlet-name>GameIdController</servlet-name>
<servlet-class>aiss.controller.GameIdController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GameIdController</servlet-name>
<url-pattern>/GameIdController</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
好吧,我想我明白为什么你会看到一个空白页面。一切都按预期工作,问题似乎与此有关:
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
您看到空白页面的原因是您的游戏变量不为空。并且你在if if阻止的第一部分中没有前向声明。
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
rd.forward(request, response); // missing this!
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
编辑:为了回应评论中的其他问题,我认为forEach因此存在问题:
<c:forEach items="${requestScope.Game}" var="game">
将其更改为:
<c:forEach items="${Game}" var="game">