【问题标题】:First JSP page--Using 2D Array--Page is not populating第一个 JSP 页面——使用 2D 数组——页面未填充
【发布时间】:2011-06-06 18:18:46
【问题描述】:

您好,

我正在尝试从头开始编写我的第一个 Java Bean + JSP 页面。但是,我使用的是一个填充了任意值的二维数组,现在当我运行 JSP 时出现异常,提示找不到数组属性:

JSP Exception: javax.el.PropertyNotFoundException: Property 'utilTableVals' not found on  type diskUtil.tester

这是我的 bean 代码:

package diskUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.lang.*;
import java.io.*;


public class tester{

//public String [][] utilTableVals;

String [][] utilTableVals = new String[20][20];

/***
bean's properties accessor
***/

/*public String[][] getUtilTableVals() { 
                return utilTableVals;
        }*/


public static String[][] getUtilTableVals()throws Exception{

tester du1 = new tester();
//String [][] utilTableVals = new String[20][20];

int i=0;
int j=0;

int row=0;
int col=0;
int result=0;


for(int r = 0; r < du1.utilTableVals.length; r++)
 {
     for(int c = 0 ; c < du1.utilTableVals[r].length; c++)
     {
        result = r+c;
          du1.utilTableVals[r][c]=Integer.toString(result);
         //System.out.print(" " + utilTableVals[r][c]);
     }
}

return du1.utilTableVals;

}//end getUtilTableVals

我的 JSP 代码在这里:

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<hmtl>
<head>
<title>Disk Utilization Page</title>
</head>
<body>
<h1>DISK UTILZATION REPORT</h1>
<br>

<jsp:useBean id="diskUtilData" scope="request" class="diskUtil.tester" />

<table>
<c:forEach var="celldata" items="${diskUtilData.utilTableVals}">
        <tr>
        <c:forEach var="col" items="${celldata}">
                <td>
                <c:out value="${col}" />
                ${col}
                <p>hello</p>
                </td>
        </c:forEach>
</c:forEach>
        </tr>


</table>
</body>
</html>

有人可以看看吗?提前致谢。

-TU

【问题讨论】:

    标签: java jsp javabeans


    【解决方案1】:

    Tester 类型的静态方法getUtilTableVals() 只能以静态方式访问。只有非静态方法会在您的 EL 表达式中被调用。

    【讨论】:

      【解决方案2】:

      getter 方法应该是publicnon static。您还应该最好在 bean 的构造函数或操作方法中进行填充,而不是在 getter 中。

      public class Tester { // Classnames ought to start with uppercase.
      
          private String[][] utilTableVals; // Properties ought to be private.
      
          public Tester() {
              utilTableVals = new String[20][20];
              // ... Preparing ought to be done in the constructor.
          }
      
          public String[][] getUtilTableVals() { // Getter ought to be public and non-static.
              return utilTableVals; // Getter should do nothing more than just returning property.
          }
      
      }
      

      最后,我强烈建议使用 Javabeans 集合而不是 2D 数组。另请参阅 Places where JavaBeans are used? 这比使用普通数组更清晰、更高效和自我记录。

      【讨论】:

        【解决方案3】:

        使getUtilTableVals() 非静态。 &lt;jsp:useBean&gt; 创建一个tester实例。当你在 EL 表达式中引用它时,它会调用一个非静态方法。

        【讨论】:

          猜你喜欢
          • 2020-09-27
          • 2012-01-30
          • 1970-01-01
          • 1970-01-01
          • 2013-12-13
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多