我有以下代码,它应该返回0到n-1之间的m个数字的排序列表。我已经验证列表已创建更正,但JSP没有打印任何内容。谁能帮我这个?这是我的动作类中的方法。
public static SortedSet<Integer> createCombo(int items, int maxNum) {
if (items > maxNum) {
System.out
.println("Cannot create a combination longer than the highest possible number.");
return null;
}
for (int i = 1; i <= items; i++) {
int newNum = 0;
boolean distinctNumber = false;
while (! distinctNumber) {
newNum = (int) Math.floor(Math.random() * maxNum);
distinctNumber = true;
if (i > 1) {
Iterator<Integer> iterator = combo.iterator();
while ((iterator.hasNext()) && (distinctNumber)) {
if (newNum == iterator.next()) {
distinctNumber = false;
}
}
}
}
combo.add(newNum);
}
printCombo();
return combo;
}
这是我的控制器类中的方法。
public String execute() {
SortedSet<Integer> combo = new TreeSet<Integer>();
try {
if ((items == 0) || (maxNum == 0)) {
return "failure";
}
combo = Combo.createCombo(items, maxNum);
if (combo != null) {
HttpSession session = (HttpSession) request.getSession();
session.setAttribute("combo", combo);
}
return "success";
} catch (Exception e) {
}
return "failure";
}
这是我的JSP。我的浏览器中只显示h1标签之间的文字。
<%@ page import="java.io.*"%>
<%@ page import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Lotto Results</title>
</head>
<body>
<center>
<h1>Lotto Results</h1>
</center>
<s:iterator value="combo">
<c:forEach var="iterator" items="${combo.iterator}" >
${iterator.next}
</c:forEach>
</s:iterator>
</body>
</html>
答案 0 :(得分:0)
combo 应该是您的Action类的属性,您应该有一个返回集合的方法getCombo()
。然后将显示值。
参见示例,我有一个名为 Song 的类和一个名为 AlbumInfoAction
的Actionpackage vaannila;
public class Song {
private String title;
private String genre;
Song(String title, String genre)
{
this.title = title;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
}
package vaannila;
import java.util.ArrayList;
import java.util.List;
public class AlbumInfoAction{
private String title;
private Artist artist;
private static List<Song> songs = new ArrayList<Song>();
static {
songs.add(new Song("Thriller","Disco"));
songs.add(new Song("Beat It","Rock"));
songs.add(new Song("Billie Jean","Pop"));
}
public String populate()
{
title = "Thriller";
artist = new Artist("Michael Jackson","King of pop");
return "populate";
}
public String execute()
{
return "success";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Artist getArtist() {
return artist;
}
public void setArtist(Artist artist) {
this.artist = artist;
}
public List<Song> getSongs() {
return songs;
}
}
要进行迭代,我应该将歌曲作为动作类的属性,并且应该存在方法 getSongs 。
JSP代码看起来像这样
<table class="songTable">
<tr class="even">
<td><b>Title</b></td>
<td><b>Genre</b></td>
</tr>
<s:iterator value="songs" status="songStatus">
<tr
class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="title" /></td>
<td><s:property value="genre" /></td>
</tr>
</s:iterator>
</table>
希望这有帮助。
答案 1 :(得分:0)
我已经解决了自己的问题。我删除了s:iterator标记,并在其中保留了迭代循环并将其更改为以下内容,并且这些更改有效。
<c:forEach var="combo" items="${combo}">
${combo}
</c:forEach>