日期:2014-05-16  浏览次数:20398 次

JSF2 EL表达式使用感受
1、对EL表达式的理解
       对于EL表达式,网上对其有很多解释,我则将其简单的理解为:模式匹配的替换,说白了就是将程序员写的一个字符串如#{bean.prop1}进行求解,然后jsf在render的时候将#{bean.prop1}替换为求解的结果。

2、要了解JSF EL的运算符
       关于此点,网上很多,大家只要明白EL可以进行算术运算,逻辑运算,关系运算,简单的字符串处理(长度、连接等)即可。
       特别注意empty的使用,在实际项目中empty通常会被大量使用。
       此外,我们还要熟练使用ui:repeat和c:foreach,ui:repeat是jsf特有的功能类似c:foreach,由于primeface的accordiontab组件不支持ui:repeat,primeface的panel等一些组件在ui:repeat下渲染出来的id莫名奇妙。因此,在jsf中还会大量使用c:foreach.

3、了解JSF EL的内嵌变量
       EL中有很多内置的变量我们可以直接使用,如request、session等,具体如下所示。
·application: ServletContext or PortletContext, The ServletContext or PortletContext, depending on whether an application is running in a servlet or portlet context, respectively.
·applicationScope: Map A map for storing application-scoped data. This is the same map into which application-scoped managed beans are stored.
·component: (JSF 2.0 only) UIComponent The UIComponent instance being currently processed. cc (JSF 2.0 only) UIComponent The top-level composite component currently being processed.
·cookie: Map A map view of values in the HTTP Set-Cookie header.
·facesContext: FacesContext The actual FacesContext instance.
·flash: (2.0 only) Map A map whose values are guaranteed to be present only on the “next” request. (A detailed explanation of flash is found in the text.)
·header Map: A map view of all the HTTP headers for this request.
·headerValues: Map A map view of all the HTTP headers for this request. Each value in the map is an array of strings containing all the values for that key.
·initParam: Map A map view of the init parameters for this application.
·param: Map A map view of all the query parameters for this request.
·paramValues: Map A map view of all the HTTP headers for this request. Each value in the map is an array of strings containing all the values for that key.
·request: ServletRequest or PortletRequest, The ServletRequest or PortletRequest, depending on whether the code is executing in a servlet or portlet environment, respectively.
·requestScope: Map A map for storing request-scoped data. This is the same scope into which request-scoped managed beans are stored.
·resource: Resource Allow a resource reference to be rendered to the page.
·session: HttpSession or PortletSession, The HttpSession or PortletSession, depending on
whether code is executing in a servlet or portlet environment, respectively.
·sessionScope: Map A map view of the session-scoped attributes.
·view: UIViewRoot The current UIViewRoot for this view.
·viewScope: (JSF 2.0 only) Map A map view of the attributes scoped to the current view.


4、JSF2 EL注意多余的空格
       当EL表达式的目标值不是String类型的时候,如为boolean,则注意表达式中不要有多余的空格。如:<p:panel rendered=” #{bean.prop1}” />,el表达式在求值时会试图” true”带有空格前缀的true进行逻辑转换,发生转换出错。

5、JSF2 EL表达式可以通过编程的方式求解,利用此,我们可以在任何一个Managed Bean中调用其它bean的方法。

6、JSF2 EL表达式的遗憾
       EL表达式或许可以使用#{} #{}等几个表达式进行连接拼接字符串,但是在#{}一个表达式中不能进行连接字符串是个遗憾。如:在c:foreach中设置了var=status,现在要往Listener中传入一个字符串,该字符串内容为: ‘str1’+status.index+’str2’,我折腾了个半小时都无果。