JSF managed bean
Hy all,
i am new with all of this,and probably this would be a dumb question,but
i have an managed bean that has a property flower - String, and a property List - bouquet
in a xhtml i have an h:inputText in witch you should type a flower name and when you click a h:command button it callas an action #{managedBEan.addFlower}, in ad开发者_如何学GodFlower i want to add the flower name typed by the user and added in list --- i am using a h:form
but it seems that the set method for property aren't called by jsf when i click the button, the flower property have null as value, like she was declared
thank you for your sugestions, Alex
Bean:
... myBean() {
Flower flower;
(...)
public void addFlower() { }
//getters and setters for Flower prop.
}
Xhtml
<h:inputText value="#{myBean.flower.name}"/>
<h:commandButton action="#{myBean.addFlower}" value="Click me!"/>
Thats all you have to do. A instance for flower with get and set and a h:inputText
with the flower name in "value" property.
When you clicks the button, the value typed will be put in the property referenced in value, so if you print the flower.name
(in addFlower method) you will be able to see the user input.
actionListener
works too, but you need to check wich import you are using for ActionEvent
. IDE always import java.awt.event.ActionEvent
but the right one is javax.faces.event.ActionEvent
... java.awt
doesn't work and I guess thats why your method is not called.
You have to declare an actionListener, like this:
<h:commandButton actionListener="#{myBean.addFlower}" . . .
and in your bean you must define addFlower
public void addFlower(ActionEvent ev)
that execute the job.
Does an tag surround both, the input and the button?
<h:form>
<h:inputText value="#{myBean.flower.name}"/>
<h:commandButton action="#{myBean.addFlower}" value="Click me!"/>
</h:form>
Otherwise the input will not be sent.
精彩评论