@ManagedBean
into another @ManagedBean
. In Java EE 6, the preferred method is to use the @ManagedProperty
annotation. You can however use JSF 2.x in Java EE 5. You must simply replace the current JSF implementation with Mojarra 2.x. If this is the case, the use of annotations is not possible. Two techniques below will work on EE5 and EE6 applications.
@MANAGEDPROPERTY
The @ManagedProperty
annotation will work on Java EE 6 applications as noted. To use the annotation, you must provide the EL name of the bean you wish to access, and provide a setter for the property in your code. Note: If you forget the setter, it will not work.
@ManagedPropery(value="#{beanName}")
private Bean bean;
public void setBean(Bean bean) {
this.bean = bean;
}
<T> T GETBEAN(STRING)
This version will work with Java EE5 and EE6.
/*
* Copyright 2011 Blue Lotus Software, LLC.
* Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
/**
* Return a reference to the bean from the {@link javax.faces.context.ExternalContext#getApplicationMap()}.
* @param <T> type of class.
* @param beanName name of the class to search for in {@link javax.faces.ExternalContext#getApplicationMap()}.
* @return referenced bean.
*/
protected <T> T getBean(String beanName) {
return (T) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(beanName);
}
}
<T> T GETBEAN(STRING BEANNAME, CLASS<T> BEANCLASS)
This version will work with Java EE5 and EE6.
/*
* Copyright 2011 Blue Lotus Software, LLC.
* Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
/**
* Return a reference to the bean from the {@link javax.faces.context.FacesContext#getCurrentInstance()}.
* @param <T> type of class.
* @param beanName name of the class to search for in {@link javax.faces.context.FacesContext}.
* @param beanClass class type of bean to return.
* @return referenced bean.
*/
protected synchronized static <T> T getBean(String beanName, Class<T> beanClass) {
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
return (T) application.evaluateExpressionGet(context, "#{" + beanName + "}", beanClass);
}
1 comments :
This is precisely why I insisted on having the separate javax.faces.bean annotations. Glad to see
someone using it. It works on tomcat 6 too.
Post a Comment