Anand Shankar
Anand Shankar

Reputation: 73

Refreshing JSF page

I have a JSF page on which I need to allow users to dynamically create a form and submit data entered in this form from the same page.

I took two design approaches for achieving it, but none of them work fully.

Approach 1: I put a p:panel component on the JSF page to act as the container of the dynamic form and bound it to my backing bean. Also, I placed commandButtons to add form elements on the page and in the action handlers of these buttons, I placed the logic of adding specific UIComponents to the panel UI component. The outcome of these action handlers, I set to the same page with faces-redirect=true. Result: The dynamic gets displayed properly, but the submit button of this dynamic form always works only on the 2nd click.

Approach 2: I put a p:panel component on the JSF page to act as the container of the dynamic form. Within this panel, I include a generated JSF page using ui:include. Similar to previous approach, in the action handlers of the command buttons to add form elements, I generate the JSF page to be included. The outcome of these action handlers, I set to the same page with faces-redirect=true. Result: The generated JSF page is not displayed immediately after the completion of action handler execution and rendering of the page. After 2 or 3 manual page refreshes, the generated JSF page gets displayed.

I guess, I am not adopting the correct design to achieve my requirement. Could you please suggest me what's going wrong in both the approaches or any alternatives.

I am using PrimeFaces and Tomcat 7.0.27.

Best regards, Anand.

Upvotes: 1

Views: 1972

Answers (1)

Ryan M.
Ryan M.

Reputation: 141

I have been developing a single page management app in JSF 2.2 with PrimeFaces 3.3 on Glassfish. You should be able to handle all your dynamic form needs with managed beans and ajax. Make sure your submit form bean is ViewScoped. Most of my JSF problems have been scope related. Each form tag should have a single purpose. Here is the structure I use for a FORM page.

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"      
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
    <h2>
       FORM ONE
    </h2>        
    <h:form>
       <p:panel> or <p:panelGrid>
    </h:form>
    <h2>
       FORM TWO
    </h2>        
    <h:form>
       <p:panel> or <p:panelGrid>
    </h:form>
</h:body>

BalusC is the king of JSF, here are his relevant entries on the subject.

http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ManagedBeanScopes

http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html

Are you using JSF 1.2 or 2.x?

http://balusc.blogspot.com/2006/06/communication-in-jsf.html

Upvotes: 1

Related Questions