java - JSTL - 添加调用参数

标签 java jstl el jstl-functions

我在 JSTL 中有以下代码,用于检索 j2ee 站点中的学生图片

 <c:if test="${student.studentPictureId != null}">
        <a href="javascript:showImage('<c:out value="${student.studentId}"/>','<c:out value="${student.studentPictureId }"/>
</c:if>

我想让代码更通用
而不是调用

 student.studentPictureId 

我想用枚举调用通用函数:
学生类的函数签名如下:

  Student class  
           String getPictureId(PictureTypeEnum picture type)

最终的 JSTL 代码如下:

  <c:if test="${student.getPictureId(PictureTypeEnum.StudentCard) != null}">
        <a href="javascript:showImage('<c:out value="${student.studentId}"/>','<c:out value="${student.getPictureId(PictureTypeEnum.StudentCard)}"/>
</c:if>

打电话时我知道

 <c:out value="${student.studentPictureId }"/>

它基本上是调用 getter,student.getStudentPictureId() 学生.studentPictureId

但是是否可以调用学生对象方法并向其传递参数?

最佳答案

不可能直接从 JSTL 调用方法,所以我使用了一种 hack:QuickMap:

public abstract class QuickMap<K,V> implements Map<K,V> {
    @Override
    public abstract V get(Object key);

    @Override
    public final int size() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final boolean isEmpty() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final boolean containsKey(Object key) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final boolean containsValue(Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final V put(K key, V value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final V remove(Object key) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final void putAll(Map<? extends K, ? extends V> m) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final void clear() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final Set<K> keySet() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final Collection<V> values() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final Set<Entry<K, V>> entrySet() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

就您而言,我会在您的学生类(class)中添加以下方法:

public Map<Object,String> getStudentPictureIds() {
    return new QuickMap<Object,String>() {
        @Override
        public String get(Object k) {
            PictureTypeEnum type;
            if (k instanceof PictureTypeEnum) {
                type = (PictureTypeEnum)k; 
            } else {
                type = PictureTypeEnum.valueOf(k.toString());
            }
            return getStudentPictureId(type);
        }            
    };
}

并不是说用 JSTL 操作枚举并不容易,因此该方法也接受字符串。

您可以在 JSP 中按如下方式使用它:

<c:out value="${student.pictureIds['StudentCard']}"/>

关于java - JSTL - 添加调用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569358/

相关文章:

java - Solaris 9 : memory leak detection

java - Parsing_Exception [match] 查询不支持 [auto_generate_synonyms_phrase_query]

java - 在 <foreach> 中使用 <form> 几次

java - JSP - 帮助在分页中生成固定数量的链接

jsf-2 - 无法在 EL/Facelet 中找到具有默认名称的 @Named CDI bean

java - 将 Retrofit 原始响应作为 json 对象返回而不转换为任何特定类型的对象?

java - Spring Batch 中的多线程

java - Jetty 如何处理具有不同依赖项的同一类的类加载?

jsf - 使用 EL 语句读取值时设置输入字段的默认值

解析 EL 值表达式的性能