JSTL——添加参数调用
JSTL - add parameters to call
我在 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
我想用枚举调用通用函数:
学生 class 的函数签名如下:
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
但是是否可以调用学生对象方法并向其传递参数?
不可能直接从 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']}"/>
我在 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
我想用枚举调用通用函数:
学生 class 的函数签名如下:
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
但是是否可以调用学生对象方法并向其传递参数?
不可能直接从 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']}"/>