javascript
jsf 开发_JSF开发人员应该知道的5种有用方法
jsf 開發
這篇文章的目的是總結一些JSF開發人員可以在日常工作中使用的便捷方法。 實用程序類是將所有方法放在一起的好地方。 我會稱此類為FacesAccessor。 第一種方法可能是最常用的方法。 它以給定名稱返回托管bean。 必須按faces-config.xml或注釋注冊該bean。 注入是好的,但是有時如果很少調用bean,則不必將bean相互注入。
使用:
@ManagedBean public class PersonBean {... }PersonBean personBean = (PersonBean)FacesAccessor.getManagedBean("personBean");// do something with personBean第二種方法對JSF組件開發人員以及想要評估給定值表達式#{…}并將結果設置為給定值的每個人都非常有用。
public static void setValue2ValueExpression(final Object value, final String expression) {FacesContext facesContext = FacesContext.getCurrentInstance();ELContext elContext = facesContext.getELContext();ValueExpression targetExpression = facesContext.getApplication().getExpressionFactory().createValueExpression(elContext, expression, Object.class);targetExpression.setValue(elContext, value); } 使用:
我個人將這種方法用于“注銷功能”。 用戶注銷后,他/她將看到一個特殊的“注銷頁面”。 “注銷頁面”使用來自sesion作用域bean的用戶設置(例如主題,語言等)。 但是由于會話無效,因此該會話作用域bean不再存在。 該怎么辦? 這是我注銷方法中的代碼片段。
第三種方法將變量映射到給定的值表達式#{…}。 它使用javax.el.VariableMapper將表達式分配給指定的變量,以便對該變量的任何引用都將被EL評估中的表達式替換。
public static void mapVariable2ValueExpression(final String variable, final String expression) {FacesContext facesContext = FacesContext.getCurrentInstance();ELContext elContext = facesContext.getELContext();ValueExpression targetExpression =facesContext.getApplication().getExpressionFactory().createValueExpression(elContext, expression, Object.class);elContext.getVariableMapper().setVariable(variable, targetExpression); } 使用:
假設“ PersonBean”是一個具有“ name”屬性的托管Bean,“ PersonsBean”是一個包含“ PersonBean”的許多實例(作為數組,集合或映射)的Bean。 以下代碼允許使用“ personBean”作為對具有“ name” Oleg的特定bean的引用。
在facelets頁面中,這樣說一下personDetail.xhtml,我們可以編寫:
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"> <ui:composition>...<h:inputText value="#{personBean.name}"/>... </ui:composition> </html>注意,參考“ personBean”是用Java設置的。 也可以通過ui:include / ui:param以聲明的方式在facelets中使用此映射。
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"> <ui:composition>...<ui:include src="personDetail.xhtml"><ui:param name="personBean" value="#{personsBean.person['Oleg']}"/></ui:include>... </ui:composition> </html>接下來的兩個方法用于以編程方式創建MethodExpression / MethodExpressionActionListener。 如果您通過“ binding”屬性使用組件綁定或在Java中創建一些模型類,則它們非常方便。
public static MethodExpression createMethodExpression(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {MethodExpression methodExpression = null;try {FacesContext fc = FacesContext.getCurrentInstance();ExpressionFactory factory = fc.getApplication().getExpressionFactory();methodExpression = factory.createMethodExpression(fc.getELContext(), valueExpression, expectedReturnType, expectedParamTypes);} catch (Exception e) {throw new FacesException("Method expression '" + valueExpression + "' could not be created.");}return methodExpression; }public static MethodExpressionActionListener createMethodActionListener(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {MethodExpressionActionListener actionListener = null;try {actionListener = new MethodExpressionActionListener(createMethodExpression(valueExpression, expectedReturnType, expectedParamTypes));} catch (Exception e) {throw new FacesException("Method expression for ActionListener '" + valueExpression+ "' could not be created.");}return actionListener; } 使用:
在我的一個項目中,我以編程方式創建了帶有菜單項的PrimeFaces MenuModel。
您想在這里分享什么好方法嗎? 歡迎使用提示/技巧。
參考: 5種有用的方法JSF開發人員應該從我們的JCG合作伙伴 Oleg Varaksin的《軟件開發博客》上了解。
翻譯自: https://www.javacodegeeks.com/2012/04/5-useful-methods-jsf-developers-should.html
jsf 開發
總結
以上是生活随笔為你收集整理的jsf 开发_JSF开发人员应该知道的5种有用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Java EE组件中使用骆驼路线
- 下一篇: 02096699是什么银行?