我试图将其更改为 ViewScoped 但每次访问属性时它都会以“调用构造函数”为 bean 结束......
同时我尝试了很多东西,没有按预期工作:
我可以让它工作的唯一情况是将控制器设置为“SessionScoped”。但我认为这不是一个好方法,因为我需要处理“清理”值并导致大量未定义(初始化)的 bean ......
我创建了一个简单的项目来玩 arround。这是有效的会话范围版本。但是如果我改变范围,我需要在点击保存按钮时“重新加载”当前用户。如果无法访问请求参数,我不知道如何实现。
(我尝试使用“表单”再次传递用户 ID,但后来我在“第一次”传递 id 时遇到问题)
这是我的样本。(Getter 和 Setter 可用,为了简短起见,没有提及)
用户等级:
public class User {
private String firstname;
private String lastname;
public User (String f, String l){
this.firstname = f;
this.lastname = l;
}
}
数据库模拟器:
@Named("dbSimulator")
@SessionScoped
public class DBSimulator implements Serializable {
/**
*
*/
private static final long serialVersionUID = 659826879566280911L;
private Map<String, User> data;
public DBSimulator() {
//Load data
User u1 = new User("Mickey", "Maus");
User u2 = new User("Peter", "Pan");
this.data = new HashMap<String, User>();
this.data.put("Mickey", u2);
this.data.put("Max", u1);
}
public List<User> getUserList(){
List<User> l = new LinkedList<User>();
for (Map.Entry<String, User> user : data.entrySet()) {
l.add(user.getValue());
}
return l;
}
public void saveUser(User user){
this.data.put(user.getFirstname(), user);
}
}
用户管理控制器:
@Named
@SessionScoped
public class UserManagementController implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4300851229329827994L;
@Inject
private DBSimulator dbSimulator;
private List<User> users;
public UserManagementController() {
System.out.println("UserManagementController:construct()");
}
@PostConstruct
public void LoadUsers(){
this.setUsers(this.dbSimulator.getUserList());
}
}
用户编辑控制器
@Named
@SessionScoped
public class UserEditController implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1867483972880755108L;
@Inject
private DBSimulator dbSimulator;
private User user;
public UserEditController() {
System.out.println("UserEditController:construct()");
}
public String activateUser(User user){
this.setUser(user);
System.out.println("setting User");
return "userEdit";
}
public String save(){
//Save user
this.dbSimulator.saveUser(user);
return "userManagement";
}
}
最后两个 XHTML 文件:UserManagement.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:outputText value="Select user to edit." />
<h:dataTable value="#{userManagementController.users}" var="user">
<h:column>
<f:facet name="header">Firstname</f:facet>
<h:outputText value="#{user.firstname}" />
</h:column>
<h:column>
<f:facet name="header">Lastname</f:facet>
<h:outputText value="#{user.lastname}" />
</h:column>
<h:column>
<f:facet name="header">Options</f:facet>
<h:form>
<h:commandLink action="userEdit" actionListener="#{userEditController.activateUser(user)}" value="edit user" />
</h:form>
</h:column>
</h:dataTable>
</h:body>
</html>
用户编辑.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{userEditController.user.firstname}"></h:inputText>
<br /><br />
<h:inputText value="#{userEditController.user.lastname}"></h:inputText>
<br /><br />
<h:commandButton value="save" action="#{userEditController.save}"></h:commandButton>
</h:form>
</h:body>
</html>
ps.:我不希望任何人“更正”我的代码 - 但也许有人能够看到完整的例子,我的 headeck 来自哪里:)
使用 ViewScoped Bean 会生成以下控制台输出:
[pageload]
12:07:49,126 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:07:49,334 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:07:49,341 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
[edit click]
12:08:35,410 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,411 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,412 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,413 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,414 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,414 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,415 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,416 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,416 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,417 INFO [stdout] (http--0.0.0.0-8090-3) UserManagementController:construct()
12:08:35,476 INFO [stdout] (http--0.0.0.0-8090-3) UserEditController:construct()
12:08:35,478 INFO [stdout] (http--0.0.0.0-8090-3) setting User
12:08:35,494 INFO [stdout] (http--0.0.0.0-8090-3) UserEditController:construct()
12:08:35,497 INFO [stdout] (http--0.0.0.0-8090-3) UserEditController:construct()
显然用户不再设置,因为(ViewScoped)UserEditController在设置用户后已经重建了两次......