0

我正在尝试在 javax.faces.view.ViewScoped 中使用 @ViewScoped 和 @Named 注释,但由于某种原因,每次我调用我的操作来更新我的部分页面时,我的 bean 都会再次实例化,但我没有得到预期结果!如果我使用 @ManagedBean 和 viewScoped(javax.faces.beans.ViewScoped),它运行良好。

另一个问题是为什么 bean 被实例化了不止一次?是因为应用程序服务器的行为吗?

这是我的xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<h:body>
<h:form id="form">
    <p:growl id="growl" showDetail="true"></p:growl>
    <p:fieldset legend="Search Criteria" toggleable="true"
        toggleSpeed="100">
        <p:panelGrid>
            <p:row>
                <p:column>
                    <p:outputLabel for="id" value="User Id">  </p:outputLabel>
                </p:column>
                <p:column>
                    <p:inputText id="id" value="#{test.id}"> </p:inputText>
                </p:column>
            </p:row>
            <p:row>
                <p:column>
                    <p:outputLabel for="username"  value="Username"></p:outputLabel>
                </p:column>
                <p:column>
                    <p:inputText id="username" value="# {test.name}"></p:inputText>
                </p:column>
            </p:row>
            <p:row>
                <p:column>
                    <p:outputLabel for="pass" value="Password"></p:outputLabel>
                </p:column>
                <p:column>
                    <p:inputText id="pass" value="#{test.password}"></p:inputText>
                </p:column>
            </p:row>
            <p:row>
                <p:column></p:column>
                <p:column>
                    <p:commandButton value="Search" action="#{test.searchAction}"
                        update="table">

                    </p:commandButton>
                </p:column>
            </p:row>

        </p:panelGrid>

    </p:fieldset>
    <p:fieldset legend="Search Result">
        <p:dataTable id="table" paginator="true" rows="20"
            value="#{test.users}" selectionMode="single" var="user"
            selection="#{test.user}" rowKey="#{user.userId}">
            <p:ajax event="rowSelect" listener="#{test.selectRow}"
                oncomplete="dialog.show();" update=":form:growl  :grid"></p:ajax>
            <p:column headerText="User ID">
                <h:outputText value="#{user.userId}" />
            </p:column>
            <p:column headerText="Username">
                <h:outputText value="#{user.userName}" />
            </p:column>
            <p:column headerText="Password">
                <h:outputText value="#{user.userPassword}" />
            </p:column>
            <f:facet name="footer">
                <p:commandButton value="Proceed" update=":grid"
                    oncomplete="dialog.show();"> </p:commandButton>
            </f:facet>

        </p:dataTable>

    </p:fieldset>

</h:form>

<p:dialog widgetVar="dialog" appendToBody="true">
    <p:panelGrid columns="2" id="grid">
        #{test.user.userId}
        <p:spacer></p:spacer>
        <p:commandButton value="Save" action="#{test.saveAction}">
        </p:commandButton>
    </p:panelGrid>
</p:dialog>


</h:body>
</html>

这是我的背豆:

package com.meysam.app.mypocekt.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;

import com.meysam.app.mypocket.jpa.User;

@Named(value = "test")
@ViewScoped
public class TestBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1275620800039726757L;
private User user;
private String name;
private Long id;
private String password;
private List<User> users;

public TestBean() {
    users = new ArrayList<>();
    System.out.println("Constructor");
}

@PostConstruct
public void init() {
    System.out.println("postConstruct");
}

public User getUser() {
    return user;
}

public String searchAction() {
    System.out.println("search Action");
    for (int i = 20; i < 40; i++) {
        User user = new User();
        user.setUserId(new Long(i));
        user.setUserName("MyName" + i);
        user.setUserPassword("password" + i);
        users.add(user);
    }
    return null;
}

public String saveAction() {
    System.out.println("next");
    return "next?faces-redirect=true";
}

public void selectRow(SelectEvent event) {

    // User user = (User) event.getObject();
    System.out.println("User: " + user.getUserName());
    FacesMessage msg = new FacesMessage("User: " + user.getUserName());
    System.out.println("selected event" + user.getUserName());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

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

public String getName() {
    return name;
}

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

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}

这是我刷新页面然后单击搜索按钮时的控制台结果:

15:43:34,667 INFO  [stdout] (default task-32) Constructor

15:43:34,668 INFO  [stdout] (default task-32) postConstruct

15:43:34,673 INFO  [stdout] (default task-32) Constructor

15:43:34,673 INFO  [stdout] (default task-32) postConstruct

15:43:34,674 INFO  [stdout] (default task-32) Constructor

15:43:34,674 INFO  [stdout] (default task-32) postConstruct

15:43:34,676 INFO  [stdout] (default task-32) Constructor

15:43:34,676 INFO  [stdout] (default task-32) postConstruct

15:43:34,677 INFO  [stdout] (default task-32) Constructor

15:43:34,677 INFO  [stdout] (default task-32) postConstruct

15:43:34,678 INFO  [stdout] (default task-32) Constructor

15:43:34,678 INFO  [stdout] (default task-32) postConstruct

15:43:34,679 INFO  [stdout] (default task-32) Constructor

15:43:34,680 INFO  [stdout] (default task-32) postConstruct

15:43:39,851 INFO  [stdout] (default task-33) Constructor

15:43:39,852 INFO  [stdout] (default task-33) postConstruct

15:43:39,852 INFO  [stdout] (default task-33) Constructor

15:43:39,853 INFO  [stdout] (default task-33) postConstruct

15:43:39,853 INFO  [stdout] (default task-33) Constructor

15:43:39,853 INFO  [stdout] (default task-33) postConstruct

15:43:39,854 INFO  [stdout] (default task-33) Constructor

15:43:39,854 INFO  [stdout] (default task-33) postConstruct

15:43:39,855 INFO  [stdout] (default task-33) Constructor

15:43:39,855 INFO  [stdout] (default task-33) postConstruct

15:43:39,855 INFO  [stdout] (default task-33) Constructor

15:43:39,856 INFO  [stdout] (default task-33) postConstruct

15:43:39,856 INFO  [stdout] (default task-33) Constructor

15:43:39,856 INFO  [stdout] (default task-33) postConstruct

15:43:39,857 INFO  [stdout] (default task-33) Constructor

15:43:39,857 INFO  [stdout] (default task-33) postConstruct

15:43:39,857 INFO  [stdout] (default task-33) Constructor

15:43:39,857 INFO  [stdout] (default task-33) postConstruct

15:43:39,858 INFO  [stdout] (default task-33) Constructor

15:43:39,858 INFO  [stdout] (default task-33) postConstruct

15:43:39,858 INFO  [stdout] (default task-33) Constructor

15:43:39,859 INFO  [stdout] (default task-33) postConstruct

15:43:39,859 INFO  [stdout] (default task-33) Constructor

15:43:39,859 INFO  [stdout] (default task-33) postConstruct

15:43:39,860 INFO  [stdout] (default task-33) Constructor

15:43:39,860 INFO  [stdout] (default task-33) postConstruct

15:43:39,861 INFO  [stdout] (default task-33) Constructor

15:43:39,861 INFO  [stdout] (default task-33) postConstruct

15:43:39,861 INFO  [stdout] (default task-33) Constructor

15:43:39,862 INFO  [stdout] (default task-33) postConstruct

15:43:39,862 INFO  [stdout] (default task-33) Constructor

15:43:39,862 INFO  [stdout] (default task-33) postConstruct

15:43:39,863 INFO  [stdout] (default task-33) Constructor

15:43:39,863 INFO  [stdout] (default task-33) postConstruct

15:43:39,863 INFO  [stdout] (default task-33) Constructor

15:43:39,863 INFO  [stdout] (default task-33) postConstruct

15:43:39,864 INFO  [stdout] (default task-33) Constructor

15:43:39,864 INFO  [stdout] (default task-33) postConstruct

15:43:39,864 INFO  [stdout] (default task-33) Constructor

15:43:39,864 INFO  [stdout] (default task-33) postConstruct

15:43:39,865 INFO  [stdout] (default task-33) Constructor

15:43:39,865 INFO  [stdout] (default task-33) postConstruct

15:43:39,865 INFO  [stdout] (default task-33) search Action

15:43:39,872 INFO  [stdout] (default task-33) Constructor

15:43:39,872 INFO  [stdout] (default task-33) postConstruct

15:43:39,872 INFO  [stdout] (default task-33) Constructor

15:43:39,873 INFO  [stdout] (default task-33) postConstruct

15:43:39,873 INFO  [stdout] (default task-33) Constructor

15:43:39,874 INFO  [stdout] (default task-33) postConstruct

15:43:39,874 INFO  [stdout] (default task-33) Constructor

15:43:39,874 INFO  [stdout] (default task-33) postConstruct

15:43:39,876 INFO  [stdout] (default task-33) Constructor

15:43:39,876 INFO  [stdout] (default task-33) postConstruct

谢谢

4

1 回答 1

1

这两个注释(CDI 和 JSF)不能一起工作。您需要编写一个扩展或使用 CODI 或 Apache DeltaSpike 来弥补差距。

JSF 2.0(ViewScope 注释的来源)没有任何 CDI 集成。

于 2013-08-12T02:52:49.107 回答