2017-08-08 53 views
0

我在JSTL下面的代碼,在J2EE站點檢索學生照片JSTL - 添加參數來調用

<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

EL 2.2中引入2009年和支持這一本身:

在你的JSP如下你會使用它。請繼續前進並追趕版本。 – BalusC

+0

如何升級eclipse項目? – user648026

回答

1

這是不可能直接調用從JSTL的方法,所以我用了一種黑客攻擊的:一個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操作枚舉不容易,所以該方法也接受字符串。

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