我刚开始学习JSP& servlet的。据我所知,我们可以从servlet设置一个session属性,并使用jsp:useBean在jsp中使用它。
现在我有一个名为Environment的自定义类,它具有某些属性。在servlet中,我正在调用一个返回List对象的服务方法。然后我设置会话属性。
List<Environment> environments = eService.fetchEnvironments();
session.setAttribute("environments", environments);
转发请求&amp;使用RequestDispatcher响应对象到目标JSP。 现在我想在JSP中访问此属性。所以我在JSP(目标JSP)中执行以下操作:
<jsp:useBean id="environments" type="java.util.List<test.project.dto.Environment>"
class="java.util.ArrayList<test.project.dto.Environment>" scope="request"></jsp:useBean>
fetchEnvironments()方法有这样的东西:
List<Environment> environmentList = new ArrayList<Environment>();
//Usual stuff
//Creating & initializing Connection, Statement, Sql string
//etc
ResultSet environments = st.executeQuery(sql);
while(environments.next())
{
Environment environment = new Environment();
environment.setEnvID(environments.getInt(1));
environment.setEnvName(environments.getString(2));
environment.setEnvDescription(environments.getString(3));
environmentList.add(environment);
}
当打开上述JSP文件时,我得到以下错误,
/EnvironmentManager.jsp (line: 31, column: 2) The value for the useBean class attribute java.util.ArrayList<test.project.dto.Environment> is invalid.
所以我的问题是我在做什么错误?我给出的“类”和“类型”属性是否正确?
为了使问题更具建设性:应该使用什么类的'class'&amp;当我有一个自定义类的List对象(List&lt; CustomClass&gt;)时,'type'属性值?
答案 0 :(得分:1)
从链接here:
属性:
class="package.class" Instantiates a bean from a class, using the new keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor. The package and class name are case sensitive.
type="package.class" If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of type must be a superclass of class or an interface implemented by class. If you use type without class or beanName, no bean is instantiated. The package and class name are case sensitive.
尝试不将类指定为:
<jsp:useBean id="environments" type="java.util.List<test.project.dto.Environment>"
scope="session"></jsp:useBean>
您使用范围作为请求。
以dst指向的jstl和el而不是jsp:useBean开始总是好的。
在jsp中导入jstl标签:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
并遍历列表:
<c:forEach items="${environments}" var="environment" varStatus="status1">
Id: <c:out value="${environment.envId}"/>
Name: <c:out value="${environment.envName}"/>
</c:forEach>
答案 1 :(得分:0)
我解决了以下类似问题:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
...
</dependencies>
在 java 中:
request.setAttribute("users", dao.getAllUsers()); // List
和 jsp
<jsp:useBean id="users" type="java.util.List<net.roseindia.bean.UserBean>" scope="request"/>
和
<c:forEach var="currentList" items="${users}">
<tr>
<td>${currentList.id}</td>
<td>${currentList.firstName}</td>
<td>${currentList.lastName}</td>
<td align="center"><a href="UserHandler?action=editform&userId=${currentList.id}">Update</a></td>
<td align="center"><a href="UserHandler?action=delete&userId=${currentList.id}">Delete</a></td>
</tr>
</c:forEach>
我希望它会有所帮助。