gpt4 book ai didi

java - 将列表转换为字符串以在 JSP 中填充网页

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:40:20 24 4
gpt4 key购买 nike

我在列表中检索了 DAO bean 行。在我的 JSP 中,我正在访问列表以遍历以填充我的页面。我的 JSP 无法访问列表,因为当我执行 request.getParameter 时它说它必须是一个字符串。我如何将其转换为 String 最终填充我的页面?

public List getAccessRequest()
{
List accessRequesttList = new ArrayList()); // parse List to string
//AccessRequest accessrequest = null;
AccessRequest accessRequest = new AccessRequest());

try
{
System.out.println("Try statement begins AccessRequestDAO");
PreparedStatement accrqststmt = super.getConnection().prepareStatement(AccRqstSqlStmt);

ResultSet resultSet = accrqststmt.executeQuery();

while (resultSet.next())
{
// Creating an instant of job follows
accessRequest = new Accessrequest();

accessRequest.setJOB_NAME(resultSet.getString("job_name"));
accessRequest.setRequest_ts(resultSet.getTime("request_ts"));
accessRequestList.add(accessRequest);
Iterator iterator = accessRequestList.iterator();

while (iterator.hasNext())
{
accessRequest = (Accessrequest) iterator.next();
}
}
return (accessRequestList);

我的 JSP 如下所示:

        <%
List jobList = request.getParameter("acccessrequests"); // parse List to String

Iterator iterator = jobList.iterator();
while (iterator.hasNext())
{
accessRequest = (AccessRequest) iterator.next());
%>
<tr>
<td><input type="checkbox" name="<%accessRequest.getApproval_ind(); %>"></td>
<td><input type="text" id="jobname' name="accessRequests" value="job_name"></td>

最佳答案

HttpServletRequest#getParameter() 返回 String , 不是 List .所以编译器是对的。

我不确定你是如何设置 List 的作为请求参数,没有像HttpServletRequest#setParameter()这样的方法.所以你可能误解了一些东西。通常的做法是通过 HttpServletRequest#setAttribute() 将列表设置为请求属性并通过 EL(表达式语言)在 JSP 中访问它,如 ${attributeName} .您通常还使用 JSTL <c:forEach> 遍历列表标签。

假设您已使用如下 Servlet 在请求范围内设置列表...

request.setAttribute("list", list);

...这是一个如何遍历列表的启动示例:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${list}" var="item">
<tr>
<td>${item.property1}</td>
<td>${item.property2}</td>
<td>${item.property3}</td>
</tr>
</c:forEach>
</table>

关于java - 将列表转换为字符串以在 JSP 中填充网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3748674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com