昨晚在晚上看了许多文章,众多大牛说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