Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect primefaces wizard to first tab after submit on last tab

Tags:

primefaces

I am using primefaces 3.2. I have prepared wizard which insert user information on same page in datatable. Wizard get information tab by tab and submitted on confirmation tab. Also it will reflected on same page on datatable. It is working fine. Now I need to update multiple users. For that I have to navigate wizard from submit button to first tab. Any help will be appreciated. My code is as below

wizard.xhtml

<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>
    <title>Wizard Example</title>
    <script type="text/javascript" >
        function resetWizard() {
            wiz.loadStep(wiz.cfg.steps[0], true);
        }
    </script>
</h:head>
<h:body>
    <h:form id="form">

        <!-- <p:growl id="growl" sticky="true" showDetail="true"/> -->
        <p:growl redisplay="false" life="3000" id="mymessage" autoUpdate="true"/>
        <p:wizard widgetVar="wiz"
                  flowListener="#{userWizard.onFlowProcess}" showNavBar="true" >

            <p:tab id="personal" title="Personal" >

                <p:panel header="Personal Details">

                    <h:messages errorClass="error"/>

                    <h:panelGrid columns="2" columnClasses="label, value" styleClass="grid">
                        <h:outputText value="Firstname: *" />
                        <p:inputText required="true" label="Firstname"
                                     value="#{userWizard.user.firstname}" />

                        <h:outputText value="Lastname: *" />
                        <p:inputText required="true" label="Lastname"
                                     value="#{userWizard.user.lastname}" />

                        <h:outputText value="Age: " />
                        <p:inputText value="#{userWizard.user.age}" />


                    </h:panelGrid>
                </p:panel>
            </p:tab>

            <p:tab id="address" title="Address" >
                <p:panel header="Adress Details">

                    <h:messages errorClass="error"/>

                    <h:panelGrid columns="2" columnClasses="label, value">
                        <h:outputText value="Street: " />
                        <p:inputText value="#{userWizard.user.street}" />

                        <h:outputText value="Postal Code: " />
                        <p:inputText value="#{userWizard.user.postalCode}" />

                        <h:outputText value="City: " />
                        <p:inputText value="#{userWizard.user.city}" />


                    </h:panelGrid>
                </p:panel>
            </p:tab>

            <p:tab id="contact" title="Contact" >
                <p:panel header="Contact Information">

                    <h:messages errorClass="error"/>

                    <h:panelGrid columns="2" columnClasses="label, value">
                        <h:outputText value="Email: *" />
                        <p:inputText required="true" label="Email"
                                     value="#{userWizard.user.email}" />

                        <h:outputText value="Phone: " />
                        <p:inputText value="#{userWizard.user.phone}"/>

                        <h:outputText value="Additional Info: " />
                        <p:inputText value="#{userWizard.user.info}"/>
                    </h:panelGrid>
                </p:panel>
            </p:tab>

            <p:tab id="confirm" title="Confirmation" >
                <p:panel header="Confirmation">

                    <h:panelGrid id="confirmation" columns="6">
                        <h:outputText value="Firstname: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.firstname}" />

                        <h:outputText value="Lastname: " />
                        <h:outputText  styleClass="outputLabel"
                                       value="#{userWizard.user.lastname}"/>

                        <h:outputText value="Age: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.age}" />

                        <h:outputText value="Street: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.street}" />

                        <h:outputText value="Postal Code: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.postalCode}" />

                        <h:outputText value="City: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.city}" />

                        <h:outputText value="Email: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.email}" />

                        <h:outputText value="Phone " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.phone}"/>

                        <h:outputText value="Info: " />
                        <h:outputText styleClass="outputLabel"
                                      value="#{userWizard.user.info}" />


                    </h:panelGrid>

                    <p:commandButton immediate="true" value="Submit" update="wiz"
                                     actionListener="#{userWizard.save}"  ajax="false"/>

                </p:panel>
            </p:tab>

        </p:wizard>



        <p:dataTable var="user" value="#{userWizard.userAll}" id="userList" editable="true" rowKey="#{user.firstname}" paginator="true"
                     rows="4" rowsPerPageTemplate="4,6" >

            <p:column headerText="FirstName" style="width:125px" filterBy="#{user.firstname}" sortBy="#{user.firstname}">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.firstname}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:outputText value="#{user.firstname}" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="LastName" style="width:125px" filterBy="#{user.lastname}" sortBy="#{user.lastname}" >
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.lastname}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{user.lastname}" style="width:100%" >
                        </p:inputText>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Age" style="width:125px" filterBy="#{user.age}" sortBy="#{user.age}">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.age}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{user.age}" style="width:100%" >
                        </p:inputText>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Edit" style="width:50px">
                <p:rowEditor />
            </p:column>
            <p:ajax event="rowEdit" listener="#{userWizard.editRowListner}" update=":form:mymessage"/>

        </p:dataTable>
    </h:form>
</h:body>

UserWizard.java

package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean
@SessionScoped

public class UserWizard {

private User user = new User();
private boolean skip;
private List<User> userAll = new ArrayList<User>();


private static Logger logger = Logger.getLogger(UserWizard.class.getName());
/*public UserWizard() {
    userAll = new ArrayList<User>();
}*/
public List<User> getUserAll() {
    return userAll;
}

public void setUserAll(List<User> userAll) {
    this.userAll = userAll;
}
public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public void save(ActionEvent actionEvent) {
    //Persist user
    System.out.println("First name : " + user.getFirstname());
    System.out.println("Last name : " + user.getLastname());
    System.out.println("Age name : " + user.getAge());
    userAll.add(user);
    user = new User();
    FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getFirstname());
    FacesContext.getCurrentInstance().addMessage(null, msg);

}

public boolean isSkip() {
    return skip;
}

public void setSkip(boolean skip) {
    this.skip = skip;
}

public String onFlowProcess(FlowEvent event) {
    logger.info("Current wizard step:" + event.getOldStep());
    logger.info("Next step:" + event.getNewStep());
    System.out.println("First name : " + user.getFirstname());
    System.out.println("Last name : " + user.getLastname());
    System.out.println("Age name : " + user.getAge());
    if (skip) {
        skip = false;   //reset in case user goes back
        return "confirm";
    } else {

        return event.getNewStep();
    }
}

public void editRowListner(RowEditEvent rowEditEvent) {
    try {
        User updatedUser = (User) rowEditEvent.getObject();
        System.out.println("User First Name: " + updatedUser.getFirstname());
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Update called", "updated by user"));
    } catch (Exception ex) {
        ex.getMessage();
    }
}
}

User.java

public class User {

private String firstname;
private String lastname;
private Integer age;
private String street;
private String city;
private String postalCode;
private String info;
private String email;
private String phone;

public User(String firstname, String lastname, Integer age, String street, String city, String postalCode, String info, String email, String phone) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.street = street;
    this.city = city;
    this.postalCode = postalCode;
    this.info = info;
    this.email = email;
    this.phone = phone;
}

public User() {

}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getInfo() {
    return info;
}

public void setInfo(String info) {
    this.info = info;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
}
like image 907
mrugeshthaker Avatar asked May 27 '12 13:05

mrugeshthaker


1 Answers

In the wizard.xhtml page change your submit button to :

<p:commandButton immediate="true" value="Submit" update="@parent,:form:userList" actionListener="#{userWizard.save}" oncomplete="wiz.loadStep (wiz.cfg.steps [0], true)"/>
like image 153
Kaalras Avatar answered Sep 23 '22 19:09

Kaalras