2010-12-06 9 views
0

私はこのような何かを持っている...JSPエル・ロジック・質問

<c:if test="${(not empty students) && (studentID != null)}"> 
     <form:input path=studentsList[${studentID}].name"> 
     .......... 
     .......... 
     //some 50+ lines of code 
</c:if> 

彼らは、特定の学生のページを表示しているときです。インクルードが..

<c:forEach items="${students}" var="student"> 
     <form:input path=student.name"> 
     .......... 
     .......... 
     //some 50+ lines of code 
</c:forEach> 

を、私はこのような何かにコードを変更するいくつかの一般的なページを表示している場合、彼らは、特定のページを表示しているかどうかを確認する文または一般的なページ

<c:when ${particularPage}> 
     <c:if test="${(not empty students) && (studentID != null)}"> 
      <form:input path=studentsList[${studentID}].name"> 
      .......... 
      .......... 
      //some 50+ lines of code 
     </c:if> 
<c:otherwise> 
     <c:forEach items="${students}" var="student"> 
      <form:input path=student.name"> 
      .......... 
      .......... 
      //some 50+ lines of code 
     </c:forEach> 
</c:otherwise> 
場合、私は持つことができます

コードを変更して50行以上を繰り返さなくてもいい方法を教えてもらえますか?

ありがとうございました

答えて

1

jsp tag fileを使用すると便利です。

<%@ attribute name="path" required="true" %> 
<form:input path="${path}"> 
//some 50+ lines of code 

をしてから呼び出すJSPでそれを呼び出す使用します:単純に属性としてform:inputパスを取ること(例えばcustom.tag、)タグを作成

<%@ taglib tagdir="/WEB-INF/tags" prefix="h" %> 
... 
    <c:when ${particularPage}> 
     <c:if test="${(not empty students) && (studentID != null)}"> 
      <h:custom path="studentsList[${studentID}].name"/>  
     </c:if> 
     <c:otherwise> 
      <c:forEach items="${students}" var="student"> 
       <h:custom path="student.name"/> 
      </c:forEach> 
     </c:otherwise> 
    </c:when> 
0

返信ありがとうございますが、私はそれを解決しました。私はscripletsを書いて、問題を解決しました。

<%for(int i = request.getAttribute("studentId") == null ? 0 : ((Integer)request.getAttribute("studentId")).intValue(); 
i< ((List)request.getAttribute("students")).size(); i++){ 

    request.setAttribute("id", new Integer(i)); 
%> 

    <form:input path=studentsList[${id}].name"> 
     .......... 
     .......... 
     //some 50+ lines of code 

<% if(!"true".equals(request.getAttribute("specificPage")){break;} 
} 
%> 

これを行う正しい方法であるかどうかはわかりません。

私がしていることが間違っている場合はお知らせください。

+1

スクリプトレットが行くのは悪い方法です。 btiernayのタグファイル解決策をもう一度見てください。 –