Anyone with JSF/Primefaces experience

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
This may be a long shot here as I don't know if many of you are Java developers.

Anyway I've been messing around with JSF/Primefaces I've created a simple page that adds "Forms" to a list and displays their names as tabs in a Accordion Panel. Or at least it's supposed to, it correctly adds a new tab for each new form that is added, but the name I give it isn't set in the form bean from the component value. It appears as null. See the image below

6MLgM.jpg


The one at the top I've hard coded to appear in the list and it's called "Form 1", the ones below are what I've added with add form part of the page, and the names of the tabs are blank, they shouldn't be.

So here is a minimal working example to recreate the issue

The XHTML

Code:
<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<title>Forms</title>
</h:head>
<h:body>
	<h:form>
		<p:accordionPanel id="formsPanel" value="#{formController.forms}"
			var="formInstance" rendered="#{not empty formController.forms}">
			<p:tab title="#{formInstance.name}">
			</p:tab>
		</p:accordionPanel>
		<p:accordionPanel rendered="#{not empty formController.forms}">
			<p:tab title="Add form">
				<p:panelGrid columns="3" layout="grid">
					<h:outputLabel value="Form name:" />
					<p:inputText value="#{formController.form.name}" required="true"
						label="text" />
					<p:commandButton update="@form"
						action="#{formController.createForm}" value="Add form" />
				</p:panelGrid>
			</p:tab>
		</p:accordionPanel>
	</h:form>
</h:body>
</html>

The FormController bean

Code:
@Named
@ViewScoped
public class FormController
{
    
    private FormService formService;
    
    private List<Form> forms;
    
    public Form form = new Form();
    
    @PostConstruct
    public void init()
    {
        formService = FormService.INSTANCE;
        forms = formService.findAll();
    }
    
    public void createForm()
    {
        formService.createForm(form);
        form = new Form();
    }
    
    public List<Form> getForms()
    {
        return forms;
    }
    
    public void setForms(List<Form> forms)
    {
        this.forms = forms;
    }
    
    public Form getForm()
    {
        return form;
    }

    public void setForm(Form form)
    {
        this.form = form;
    }
    
}

The FormService (I'm well aware this is badly written, I've created this "service" just to allow for something to be easily demonstratable)

Code:
@Named
public enum FormService
{
    
    INSTANCE(new ArrayList<Form>());
    
    private List<Form> forms;
    
    private FormService(List<Form> forms)
    {
        this.forms = forms;
        this.forms.add(new Form("Form 1"));
    }
    
    public void createForm(Form form)
    {
        forms.add(form);
    }
    
    public List<Form> findAll()
    {
        return forms;
    }

}

And the Form (very simple)

Code:
public class Form
{
    
    private String name;
    
    public Form()
    {
        
    }
    
    public Form(String name)
    {
        this.name = name;
    }

     public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
    
}

What I've done is place a couple of break points one in the Form.setName and one in FormController.createForm.

When typing in a name of a form and clicking Add Form, both of these methods are called.

First of all setForm is called with a Form object reference (for example) com.test.Form@30998fa6 and it's name is correct.

Then createForm is called with a Form object reference com.test.Form@2ad84d0f and the name is null. What I don't get is why these are apparently two different form objects.
 
Back
Top Bottom