【问题标题】:String Array (String[ ][ ]) output in JSPJSP 中的字符串数组 (String[ ][ ]) 输出
【发布时间】:2016-03-17 15:47:41
【问题描述】:

我对 JSP 完全陌生。我想要的是基本上用 JSP 打印一个 String[][],就像我之前在 Eclipse 中的控制台上所做的那样:

public void printField(String[][] field){
        for (int i = 0; i < field.length-1; i++){
            for (int j = 0; j < field[i].length-1; j++){
                String x = field[j][i];
                System.out.print("|");

                if (x.equals(" ")){
                    System.out.print("   ");
                } else if (x.equals("S")) {
                    System.out.print(" S ");
                } else if (x.equals("x")) {
                    System.out.print(" x ");
                } else if (x.equals("#")){
                    System.out.print(" # ");
                } else if (x.equals("*")){
                    System.out.print(" * ");
                }
                else if (x.equals("10")){
                    System.out.print(" "+x );
                }
                else {
                    System.out.print(" " +x +" ");
                }
            }
            System.out.println("|");
        }
        System.out.println();
}

它会这样:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|
| 1 | S |   |   | S | S | S |   | S |   |   |
| 2 | S |   |   |   |   |   |   | S |   |   |
| 3 |   | S |   |   |   |   |   |   |   |   |
| 4 |   | S |   | S |   |   |   |   |   |   |
| 5 | S |   |   | S |   |   | S |   | S |   |
| 6 | S |   |   | S |   |   | S |   | S |   |
| 7 | S |   |   | S |   |   | S |   | S |   |
| 8 | S |   |   |   |   |   | S |   |   | S |
| 9 |   |   |   |   |   |   | S |   |   | S |
| 10|   |   |   | S | S | S |   |   |   |   |

正如一般信息:我已经编码了战舰。我现在想做的是创建一个基于网络的前端,这就是我问的原因。稍后我将用图形项替换此字段输出。但这只是为了学习目的。

【问题讨论】:

  • 你可以使用像pbr这样的html标签来制作html内容而不是sout
  • 您必须了解 stackoverflow 的工作原理... a)您无法回答自己进行更新,您必须编辑原始问题,b)您现在问的是一个完全不同的问题,原始问题是关于 jsp 并将数据传递给请求,现在您在构建 html 和 c) 时遇到问题,如 cmets 中所述,我给您的是指南,正如您在结果中看到的那样,未测试的方法是即时编写的 现在你必须努力获得想要的设计或提出一个新问题。

标签: java arrays string jsp printing


【解决方案1】:

首先。您是否意识到所有这些条件都是完全不必要的???

if (x.equals(" ")){
    System.out.print("   ");
} else if (x.equals("S")) {
    System.out.print(" S ");
} else if (x.equals("x")) {
    System.out.print(" x ");
} // others....

您取值并先添加一个空格,然后...然后您的代码与以下内容相同:

if (x.equals("10")){
    System.out.print(" "+x );
} else {
    System.out.print(" " + x + " ");
}

你也可以使用String.format...但无论如何...


在 JSP 中显示此结构:

在请求中设置你的属性

request.setAttribute("field", field);

然后在JSP中:

<table class="your_class">
    <thead>
        not necessary
    </thead>

然后用 :

遍历元素
    <tbody>
        <!-- each item in field will be an 1d array -->
        <c:forEach items="${field}" var="row">
            <td> |
                <tr class="your_class">
                <!-- each item in row will be an element -->
                <c:forEach items="${row}" var="item">
                    ${item} |
                </c:forEach>
                </tr>
            </td>
        </c:forEach>
    </tbody>
</table>

【讨论】:

  • 我不确定 JSP 代码是否可以工作。每次您输出${item} 时,您都会使用它创建一个新行。我以前做过类似的事情,但有点棘手。行创建需要放在循环之间。
  • @RaymondHolguin 同意,只是标记了操作的路径,但它是即时编写的未经测试的代码
  • 别担心,我明白这一点。只是因为 OP 对 JSP 来说是新手,所以他可能认为这是一个可行的示例,因为您的答案中没有指出这只是一个粗略的起点。
  • 好的,更正了,希望就在眼前……仍在飞行中 xD
猜你喜欢
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2021-11-28
  • 2012-08-06
  • 1970-01-01
相关资源
最近更新 更多