gpt4 book ai didi

java - 无法使用 JSTL 打印出列表对象内容

转载 作者:行者123 更新时间:2023-11-29 00:07:24 25 4
gpt4 key购买 nike

我正在从 Mysql 数据库中检索行并将每一行作为一个对象存储在列表中。我将列表传递到请求范围,我在其中尝试使用 JSTL 遍历列表并访问对象变量。

以下是我如何将行检索为对象:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

// get list of objects
List<Game> gameRatingsThisWeek = getHighestRatedGameThisWeek();
request.setAttribute("gameRatingsThisWeek", gameRatingsThisWeek);

RequestDispatcher rd = getServletContext().getRequestDispatcher("/dashboard.jsp");
rd.forward(request, response);

private List<Game> getHighestRatedGameThisWeek() {
List<Game> list = new ArrayList<Game>();
try(Connection con = ds.getConnection()){
String query = "SELECT overallRating, count(*) as Total, g.name"
+"FROM gameSurvey gs, game g"
+"WHERE g.id = gs.gameID AND"
+"date BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()"
+"GROUP by overallRating"
+"ORDER by Total desc";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Game game = new Game();
game.setName(rs.getString("name"));
game.setRating(rs.getInt("overallRating"));
game.setTotal(rs.getInt("Total"));
list.add(game);
}
ps.close();

}catch(SQLException e){}
return list;

}

}

我知道 SQL 查询没问题,因为我之前测试过它。

这是游戏类:

public class Game {
private int rating, total;
private String name;

// get
public int getRating(){
return rating;
}
public int getTotal(){
return total;
}
public String getName(){
return name;
}

// set
public void setRating(int rating){
this.rating = rating;
}
public void setTotal(int total){
this.total = total;
}
public void setName(String name){
this.name = name;
}
}

在 dashboard.jsp 文件中,我尝试遍历列表:

<c:forEach items="${gameRatingsThisWeek}" var="game">
<c:out value="${game.name}"></c:out>
</c:forEach>

我导航到检索并转发到 .jsp 页面的 servlet。但是没有任何内容输出到网页。

最佳答案

您当前的查询在每个字符串文字中都缺少空格。这意味着您的数据库看到以下语句:

SELECT overallRating, count(*) as Total, g.nameFROM gameSurvey gs, game gWHERE g.id = gs.gameID ANDdate BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()GROUP by overallRatingORDER by Total desc

只需添加空格并再试一次。

        String query =   "SELECT overallRating, count(*) as Total, g.name "
+"FROM gameSurvey gs, game g "
+"WHERE g.id = gs.gameID AND "
+"date BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() "
+"GROUP by overallRating "
+"ORDER by Total desc";

关于java - 无法使用 JSTL 打印出列表对象内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com