栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在具有选定值的jsp / jstl中进行多重选择?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在具有选定值的jsp / jstl中进行多重选择?

我将为此创建一个EL函数。

package com.example;import java.util.Collection;public final class Functions {    private Functions() {        //    }    public static boolean contains(Collection<Object> collection, Object item) {        return collection.contains(item);    }}

/WEB-INF/functions.tld
像这样定义

<?xml version="1.0" encoding="UTF-8" ?><taglib     xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"    version="2.1">    <display-name>Custom Functions</display-name>        <tlib-version>1.0</tlib-version>    <uri>http://example.com/functions</uri>    <function>        <name>contains</name>        <function-class>com.example.Functions</function-class>        <function-signature>boolean contains(java.util.Collection, java.lang.Object)</function-signature>    </function></taglib>

然后您可以按以下方式使用它

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@taglib uri="http://example.com/functions" prefix="f" %>...<select name="roles" multiple>    <c:forEach items="${allRoles}" var="role">        <option value="${role.id}" ${f:contains(user.roles, role) ? 'selected' : ''}>${role.name}</option>    </c:forEach></select>

更新
:为了集合中恰当地比较对象,必须实施

equals()
hashCode()
相应。您似乎还没有这样做。这是一个按技术ID进行比较的基本示例:

public boolean equals(Object other) {    return other instanceof Role && id != null ? id.equals(((Role) other).id) : other == this;}public int hashCode() {    return id != null ? getClass().hashCode() + id.hashCode() : super.hashCode();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号