@Named アノテーションを使用して javax.faces.view.ViewScoped で @ViewScoped を使用しようとしていますが、何らかの理由でアクションを呼び出してページの一部を更新するたびに、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
ありがとうございました