迭代List< CustomClass>在jsp中(jsp:getProperty?) [英] Iterating through a List<CustomClass> in jsp (jsp:getProperty?)

查看:107
本文介绍了迭代List< CustomClass>在jsp中(jsp:getProperty?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想迭代一个List,它是User对象的成员变量。
我不想使用片段,并希望使用某种形式的jsp标签来实现这一目的。

I want to iterate through a List that is a member variable of a User object. I don't want to use snippets and would like to use some form of jsp tags to do the trick.

用户等级

public class User {

  private List<Option> options;

  public getOptions()...
}

我在片段中尝试做什么

<%
User user = (User)session.getAttribute("user");
List<Option> options = user.getOptions();
%>

<select id="alertFilter">

<% for (Option o : options) { %>

<option><%=o.getTitle()%></option>

<% } %>

</select>

我已经看到了一些我想要做的简单例子,但它们总是变得简单退回。

I've seen a few simple examples of what I'm trying to do but they always get simple ojects back.

标记库方式 - 无法正常工作

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

<jsp:useBean id="user" class="ie.openmobile.smsjobs.entity.User" scope="request"></jsp:useBean>

<c:forEach var="options" items="$user.options" >   <--incorrect references to alerts/getOptions()

<br>$options.title   <--incorrect syntax

</c:forEach>

任何人都可以帮助我吗?

Can anyone help me out?

推荐答案

将其更改为如下

<select id="alertFilter">

<c:forEach var="option" items="${user.options}" >
  <option><c:out value="${option.title}"/></option>
</c:forEach>

</select>

解释:$ {user.options}将从会话中获取用户和使用 getOptions()方法的选项集合,它将迭代每个条目

Explanation : ${user.options} will get the user from session and options collection using its getOptions() method, and it will iterate on each entry

在迭代中 $ {option.title} 将提供选项(这是遍历下的当前选项实例)和 option.getTile()

While under the iteration ${option.title} would give the option ( which is the current option instance under traversal) and option.getTile()

这篇关于迭代List&lt; CustomClass&gt;在jsp中(jsp:getProperty?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆