Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何从列表创建select元素?_Jsp_Spring Mvc - Fatal编程技术网

Jsp 如何从列表创建select元素?

Jsp 如何从列表创建select元素?,jsp,spring-mvc,Jsp,Spring Mvc,有一个DAO方法返回一个列表: @Transactional public List<Object[]> list() { String sql = "select pnd_code, to_char(pnd_intitule) l from pnd order by l"; // pnd_intitule is a CLOB column Query query = sessionFactory.getCurrentSession().createSQLQuer

有一个DAO方法返回一个列表:

@Transactional
public List<Object[]> list() {

    String sql = "select pnd_code, to_char(pnd_intitule) l from pnd order by l"; // pnd_intitule is a CLOB column

    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);

    @SuppressWarnings("unchecked")
    List<Object[]> list = (List<Object[]>) query.list();

    return list;

}
在我的JSP中,我想从中创建一个select元素;以下是我尝试过但在运行时崩溃的内容:

<select id="pnd" style="width:500px;">
    <option value=""> -- S&eacute;lectionner -- </option>
    <c:forEach items="${pnds}" var="pnd">
        <option value="${pnd.pnd_code}">${pnd.l}</option>
    </c:forEach>
</select>

那么在这种情况下如何创建select元素呢?

请帮自己一个忙,创建一个封装数据的自定义类

class MyCustomClass{ // change name
    private final String lecturer;
    private final String code:
    // initialize fields in constructor
    // add getters
}
DAO类:

public List<MyCustomClass> list() {

    String sql = "select pnd_code, to_char(pnd_intitule) l from pnd order by l"; // pnd_intitule is a CLOB column

    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);

    @SuppressWarnings("unchecked")
    return ((List<Object[]>) query.list())
         .stream()
         .map(oo -> new MyCustomClass(oo[0].toString(), oo[1].toString()))
         .collect(Collectors.toList());

}
旧式Java 7版本:

public List<MyCustomClass> listWithoutLambdasAndStreams() {

    String sql = "select pnd_code, to_char(pnd_intitule) l from pnd order by l"; // pnd_intitule is a CLOB column

    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);

    @SuppressWarnings("unchecked")
    List<Object[]> list = (List<Object[]>) query.list();
    List<MyCustomClass> youShouldReallyLearnAboutLambdasAndStreams =
        new  ArrayList<>(list.size());
    for(Object[] oo: list){
        youShouldReallyLearnAboutLambdasAndStreams.add(
            new MyCustomClass(oo[0].toString(), oo[1].toString()));
    };
    return youShouldReallyLearnAboutLambdasAndStreams;
}
JSP:


有错误:Lambda表达式只允许在源代码级别1.8或更高版本,即使我将项目jre系统库设置为1。8@pheromix然后切换到源代码级别1.8或重构我的代码以不使用lambdascan。请重构它,因为我不知道lambda表达式。@pheromix完成,但请帮自己一个忙,学习Java8Lambdas和streams。一旦你理解了它,生活就会变得容易多了!
<select id="pnd" style="width:500px;">
    <option value=""> -- S&eacute;lectionner -- </option>
    <c:forEach items="${pnds}" var="pnd">
        <option value="${pnd.code}">${pnd.lecturer}</option>
    </c:forEach>
</select>