我要从servlet传递到jsp的对象数组

我在object上有自己的班级com.example

class object {

String param1;

//getters and setters

}


我的servlet代码:

object[] sampleObject = new object[5];

// code to populate object

RequestDispatcher dispatch = request.getRequestDispatcher("/inc/example.jsp");
request.setAttribute("object", sampleObject);
dispatch.forward(request, response);


我的example.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
    <%@ page import="java.util.*" %>
 <%@ page import="com.example.object" %>
    <jsp:useBean id="object" scope="request" class="java.util.Arrays" />

     <%
     int l = object.length;
    %>


这失败,并显示错误The value for the useBean class attribute java.lang.Arrays is invalid

当我尝试

<jsp:useBean id="object" scope="request" class="com.example.object" />


我得到的错误是

The type of the expression must be an array type but it resolved to object


它仍然失败。我应该如何配置我的jsp以使用this。

我在jsp:useBean中为object定义的类应该是什么。它因选择java.util.Arrays为无效而大喊大叫,但当我使用com.example.object时也大喊大叫

最佳答案

尝试这个:

<jsp:useBean id="object" scope="request" class="java.lang.Object" />


至少您将能够获取数组,然后使用显式强制转换来获取特定属性。但是最好使用EL标签来遍历数组。

10-04 11:38
查看更多