2013-12-20 58 views
0

我正在處理JSP標記。這裏是行開始通過項目模型中的循環:如何評估動態/嵌套的Spring屬性佔位符表達式?

<c:forEach var="toc" items="${requestScope[formKey].model.sharingTocs}"> 

但代碼已被重構,因此模型路徑(以上model.sharingTocs)現在是動態的,而不是固定的。現在通過JSP @attribute傳遞到代碼:

<%@attribute name="path" required="true"%> 

所以${path}現在計算爲"model.sharingTocs"

items現在如何分配?

回答

1

好吧。好問題。

這是一個解決方案:編寫自定義JSTL標記來評價一個bean的屬性表達式:

<mytag:eval bean="${requestScope['formKey']}" propertyExpression = "${path}" var="items" /> 

和foreach:mytag的

<c:forEach var="toc" items="${items}"> 
</c:forEach> 

示例代碼:EVAL JSTL標籤(經典款)

public class EvalTag extends TagSupport 
{ 

    private Object bean; 
    private String propertyExpression; //Ex: 'model.sharingTocs' 
    private String var; 

    //............ 


    @Override 
    public int doEndTag() throws JspException { 
     try { 

      // Use reflection to eval propertyExpression ('model.sharingTocs') on the given bean 

      Object propObject = SomeLibs.eval (this.bean, this.propertyExpression); 

      this.pageContext.getRequest().setAttribute(this.var, propObject); 

      // You can add propObject into Other scopes too. 

     } catch (Exception ex) { 
      throw new JspTagException(ex.getMessage(), ex); 
     } 
     return EVAL_PAGE; 
    } 

     //............ 
     // SETTERS here 
} 

您可以使用一個庫來評估bean的bean表達式是Apache bean的utils。

http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#standard.nested

0

如果你正在使用Spring,你可以使用Spring標記庫,它也假定你是一個表單中:形態標記。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

<%@ attribute name="path" required="false" %> 

<spring:bind path="${path}"> 
    <c:forEach var="item" items="${status.value}"> 
      ${item} 
    </c:forEach> 
</spring:bind> 
0

它已經相當一段時間,因爲我問過這個問題,但(與因爲新獲得的知識),我認爲這應該工作:

<c:set var="itemsPath" value="requestScope[formKey].${path}"/> 
<c:forEach var="toc" items="${itemsPath}"> 

即設立中介JSTL變量的完整路徑對項目進行評估並對​​其進行評估。