如何在最后一个选项卡上提交后将primefaces向导重定向到第一个选项卡

时间:2012-05-27 13:41:23

标签: primefaces

我正在使用primefaces 3.2。我准备了向导,在datatable中的同一页面上插入用户信息。向导按选项卡获取信息选项卡并在确认选项卡上提交。它也会反映在datatable的同一页面上。它工作正常。现在我需要更新多个用户。为此,我必须将向导从提交按钮导航到第一个选项卡。 任何帮助将不胜感激。 我的代码如下

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;
}
}

3 个答案:

答案 0 :(得分:13)

在wizard.xhtml页面中,将提交按钮更改为:

<p:commandButton immediate="true" value="Submit" update="@parent,:form:userList" actionListener="#{userWizard.save}" oncomplete="wiz.loadStep (wiz.cfg.steps [0], true)"/>

答案 1 :(得分:5)

在primefaces 5中你无法直接调用widgetVar,你需要像PF('widgetVar')一样调用它,所以你可以这样做:

<p:commandButton value="Submit" actionListener="#{userWizard.save}" oncomplete="PF('wiz').loadStep('tabId',false)" />

答案 2 :(得分:5)

您也可以从Java代码中执行以下操作:

    Wizard wizard = (Wizard) FacesContext.getCurrentInstance().getViewRoot().findComponent("importForm:wizardId");
    wizard.setStep(STEP1);
    RequestContext.getCurrentInstance().update("importForm");