JSTL - 添加要调用的参数

时间:2017-08-08 13:12:53

标签: 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 

我想用Enum调用泛型函数:
学生班上的功能签名如下:

  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() student.studentPictureId

但是可以调用学生对象方法并将参数传递给它吗?

1 个答案:

答案 0 :(得分:1)

直接从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.");
    }
}

在您的情况下,我会在您的学生班中添加以下方法:

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']}"/>