0

我是 JSF 的新手。我正在尝试使用 Primefaces 官方网站上的向导组件,但我遇到了一个问题:当移动到下一个选项卡时,前一个选项卡的信息会被删除。我使用了网站上的代码,除了 User bean。当我进行调试时,我看到每次按下 Next 按钮时,都会调用每个字段的 setter,但在前面的步骤中设置的字段为 null。我正在使用 primefaces 3.5 版本和 jsf-api 和 jsf-impl 2.1.18。

这是代码:

用户向导:

@ManagedBean(name="userWizardBean")
@SessionScoped
public class UserWizard {

private User user = new User();
private boolean skip;

private static Logger logger = Logger.getLogger(UserWizard.class.getName());

public User getUser() {
    return user;
}

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

public void save(ActionEvent actionEvent) {
    //Persist 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());

    if(skip) {
        skip = false;   //reset in case user goes back
        return "confirm";
    }
    else {
        return event.getNewStep();
    }
}

}

用户:

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

和 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>
</h:head>
<h:body>
  <h:form>

    <p:growl id="growl" sticky="true" showDetail="true"/>

    <p:wizard widgetVar="wiz"
              flowListener="#{userWizard.onFlowProcess}">

        <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:outputText value="Skip to last: " />
                    <h:selectBooleanCheckbox value="#{userWizard.skip}" />
                </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:outputText value="Skip to last: " />
                    <h:selectBooleanCheckbox value="#{userWizard.skip}" />
                </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:outputText />
                    <h:outputText />
                </h:panelGrid>

                <p:commandButton value="Submit" update="growl"
                                 actionListener="#{userWizard.save}"/>

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

    </p:wizard>

 </h:form>
</h:body>
</html>

任何帮助或提示将不胜感激。谢谢

4

2 回答 2

2

我已经设法使它工作:刚刚添加:@ManagedBean(name = "userWizard") 和 @ViewScoped 到 UserWizard 类,它工作。

于 2013-03-30T14:03:43.073 回答
0

实际上问题出在范围上,因为您提到了会话范围,因此数据仅存储用于单个会话,它应该是视图范围,因为视图范围数据保持存储,除非您被重定向到另一个页面。

我希望它对你有帮助。

于 2015-04-09T12:43:36.327 回答