昨晚在晚上看了許多文章,衆多大牛說OGNL性能不行雲雲,乍一看似乎慘不忍睹,如下圖:
於是考慮是否能使用EL+JSTL代替實現前臺的標籤。
以最近測試用的簡單留言板的查看文章頁面爲例,以下皆省略getter,setter方法:
ViewArticle:
public class ViewArticle extends ActionSupport { private static final long serialVersionUID = 77L; private ArticleService as; private int page = 1; @Override public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); ctx.put("articles", as.getAllArticle(page)); return SUCCESS; }
前臺頁面,OGNL+Struts-tags實現:
<table> <s:iterator value="#request.articles" id="a" status="s"> <tr> <td><s:property value="#s.index"/></td> <td class="title"><s:property value="#a.title"/></td> <td class="content"><s:property value="#a.content"/> </td> <td class="username"><s:property value="#a.user.username"/></td> <td><s:a action="deletePro?id=%{#a.id}">刪除</s:a></td> </tr> </s:iterator> </table>
EL+JSTL實現:
<table> <c:forEach items="${articles}" var="a" varStatus="s"> <tr> <td><c:out value="${s.index}"/></td> <td class="title"><c:out value="${a.title}"/></td> <td class="content"><c:out value="${a.content}"/></td> <td class="username"><c:out value="${a.user.username}"/><br></td> <td><a href="deletePro?id=${a.id}">刪除</a></td> </tr> </c:forEach> </table>
可以看出,兩者的複雜度和可讀性並沒有多大的差別,可能我這個Demo比較簡單,從代碼上來看EL+JSTL並沒有簡潔多少,不過Struts-Tgas若不使用simple風格的話,自動生成的HTML代碼有點慘不忍睹,從生成的代碼簡介角度來看,我並不很傾向於使用Strust-Tags。
但是Sturts-Tags在國際化方面有着很大的優勢,如下圖:
<s:text name="username"/> <s:text name="password"/>
可以很方便的讀取出國際化文件中的資源,而不使用此方法的話要如何實現還沒有深入研究。
考慮到之前的ONGL效率問題,以及EL和JSTL的官方性,在之後的項目中可能更加傾向於使用EL+JSTL的方式構建前臺頁面。
下面有幾篇EL和JSTL學習的文章,MARK一下:
JSTL 學習、應用記錄
http://www.blogjava.net/JAVA-HE/archive/2007/05/27/120344.htm
EL表達式
http://www.cnblogs.com/Fskjb/archive/2009/07/05/1517192.html
JSTL <c:ForEach/>
http://www.2cto.com/kf/201109/103832.html
OGNL與JSTL對比
http://newleague.iteye.com/blog/730898
EL表達式詳解
原文鏈接:http://www.cnblogs.com/coolhwm/archive/2011/11/25/2263519.html