JSTL 不打印值

发布于 2024-12-29 13:28:48 字数 2179 浏览 0 评论 0原文

我有以下代码,应该返回 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>

I have the following code that is supposed to return a sorted list of m numbers between 0 and n - 1. I have verified that the list is created corrected, but the JSP is not printing anything. Can anyone help me with this? This is the method in my action class.

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;
}

This is the method in my controller class.

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";
}

This is my JSP. Only the text between the h1 tags appears in my browser.

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2025-01-05 13:28:48

combo 应该是 Action 类的一个属性,并且您应该有一个返回集合的方法 getCombo() 。然后将显示这些值。

请参阅示例,我有一个名为 Song 的类和一个名为 AlbumInfoAction 的操作。

package 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>

希望这会有所帮助。

combo should be a property of your Action class and you should have a method getCombo() which returns the collection. Then the values will be displayed.

See the example, I've a class called Song and an Action called AlbumInfoAction

package 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;
    }

}

To iterate, I should have songs as a property of the action class and a method getSongs should be present.

The JSP code would look like this

<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>

Hope this helps.

当梦初醒 2025-01-05 13:28:48

我已经解决了我自己的问题。我删除了 s:iterator 标签,并将迭代循环保留在其中并将其更改为以下内容,并且这些更改起作用了。

<c:forEach var="combo" items="${combo}">
    ${combo}
</c:forEach>

I have solved my own problem. I removed the s:iterator tag, and I kept the iteration loop inside it and changed it to the following, and these changes worked.

<c:forEach var="combo" items="${combo}">
    ${combo}
</c:forEach>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文