You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Hendrik Neumann <th...@googlemail.com> on 2006/07/23 19:15:05 UTC
How can I create Managed Beans during runtime?
Hi everybody,
currently I'm developing a JSF-component which renders a set of
input-elements:
public void encodeBegin(FacesContext myContext) throws IOException
{
[...]
// create input-Elements
HtmlInputText cssAttributeNameInput = (HtmlInputText)
myApp.createComponent(HtmlInputText.COMPONENT_TYPE);
HtmlInputTextarea cssAttributeDescriptionInput = (HtmlInputTextarea)
myApp.createComponent(HtmlInputTextarea.COMPONENT_TYPE);
[...]
}
Now I need to bind these input-fields with this JavaBean:
public class BaseProperty
{
private String name;
private String description;
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Normally, this would be no problem, because I would define a managed-bean in
the faces-config.xml and use the createValueBinding-Method from the
ApplicationFactory to bind my JSF-Input-components to the attribute values
of my Bean.
The problem now is: I want to create an independent JSF-component which
should be deployed in a JAR-file and which should be usable in any
JSF-Web-Application without adding specific managed-beans to the local
faces-config.xml. Therefore I need to dynamicly create a managed bean from
my "BaseProperty"-class during runtime.
I think about something like
createManagedBean("temporaryBean", "
de.rub.swt.csseditor.model.BaseProperty", "session");
ValueBinding dynBinding = myApplicationFactory.createValueBinding("
temporaryBean.name");
cssAttributeNameInput.setValue(dynBinding);
Is this possible? What must be the content of
public void createManagedBean(String managedBeanName, String
managedBeanClass, String scope)
{
// ???????????
}
Any hints or ideas?
Greetings,
Hendrik
Re: How can I create Managed Beans during runtime?
Posted by Hendrik Neumann <th...@googlemail.com>.
Hi Matthias,
Why not adding the MB to the faces-config of your component's
> faces-config.xml file?
I already thought about such a solution as a "emergency plan", but I would
prefer the generic solution.
A managed bean is just a simple javaBean which can be looked up from
> the VariableResolver. The method resolveVariable(faces_context,
> name_of_my_bean) goes through all the scopes and looks it up & returns
> the bean than. Or create's it using the ManagedBeanBuilder, if not
> attached to any scope yet.
>
> So why not just creating a new object and storing it under the needed
> context ?
> like fc.getExternalContext().getSessionMap().put("fooBean", obj");
I think this were excactly the remarks I hoped for to solve my problem -
thanks a lot :-)
Greetings,
Hendrik
Re: How can I create Managed Beans during runtime?
Posted by Matthias Wessendorf <ma...@apache.org>.
> Normally, this would be no problem, because I would define a managed-bean in
> the faces-config.xml and use the createValueBinding-Method from the
> ApplicationFactory to bind my JSF-Input-components to the attribute values
> of my Bean.
>
> The problem now is: I want to create an independent JSF-component which
> should be deployed in a JAR-file and which should be usable in any
> JSF-Web-Application without adding specific managed-beans to the local
> faces-config.xml. Therefore I need to dynamicly create a managed bean from
> my "BaseProperty"-class during runtime.
Why not adding the MB to the faces-config of your component's
faces-config.xml file?
> I think about something like
>
> createManagedBean("temporaryBean", "
> de.rub.swt.csseditor.model.BaseProperty", "session");
> ValueBinding dynBinding =
> myApplicationFactory.createValueBinding("temporaryBean.name");
> cssAttributeNameInput.setValue(dynBinding);
>
> Is this possible? What must be the content of
>
> public void createManagedBean(String managedBeanName, String
> managedBeanClass, String scope)
> {
> // ???????????
> }
A managed bean is just a simple javaBean which can be looked up from
the VariableResolver. The method resolveVariable(faces_context,
name_of_my_bean) goes through all the scopes and looks it up & returns
the bean than. Or create's it using the ManagedBeanBuilder, if not
attached to any scope yet.
So why not just creating a new object and storing it under the needed context ?
like fc.getExternalContext().getSessionMap().put("fooBean", obj");
-Matt
> Any hints or ideas?
>
> Greetings,
> Hendrik
>
--
Matthias Wessendorf
further stuff:
blog: http://jroller.com/page/mwessendorf
mail: mwessendorf-at-gmail-dot-com