Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 2.0 backing bean method not executed for programmatically created UI element
    text
    copied!<p>I am using JSF's java classes to create UI elements for a simple HTML page as shown below. However the methods Bean::save() and Item.setValue(...) aren't executed.</p> <p>item.xhtml</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h:panelGroup binding="#{bean.htmlPanelGroup}" /&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>Bean.java</p> <pre><code>@ManagedBean(name="bean") @ViewScoped public class Bean implements Serializable { private final Item item = new Item(1L, "hello"); public void save() { // this method is not executed when the save button created below is clicked. System.out.println("item.value = " + item.getValue()); } public Item getItem() { return this.item; } transient HtmlPanelGroup htmlPanelGroup; public void setHtmlPanelGroup(HtmlPanelGroup htmlPanelGroup) { this.htmlPanelGroup = htmlPanelGroup; } public HtmlPanelGroup getHtmlPanelGroup() { if (htmlPanelGroup == null) { FacesContext facesContext = FacesContext.getCurrentInstance(); htmlPanelGroup = new HtmlPanelGroup(); HtmlForm editForm = new HtmlForm(); HtmlInputText value = new HtmlInputText(); ValueExpression valueExpr = facesContext.getApplication().getExpressionFactory() .createValueExpression(facesContext.getELContext(), "#{bean.item.value}", String.class); value.setValueExpression("value", valueExpr); editForm.getChildren().add(value); HtmlCommandButton save = new HtmlCommandButton(); save.setValue("save"); MethodExpression methodExpr = facesContext.getApplication().getExpressionFactory() .createMethodExpression(facesContext.getELContext(), "#{bean.save}", String.class, new Class&lt;?&gt;[]{}); save.setActionExpression(methodExpr); editForm.getChildren().add(save); htmlPanelGroup.getChildren().add(editForm); } return htmlPanelGroup; } } </code></pre> <p>Item.java</p> <pre><code>public class Item implements Serializable { private long id; private String value; public Item(long id, String value) { this.id = id; this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public long getId() { return id; } public void setId(long id) { this.id = id; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload