<分区>
我认为 @ViewScoped
应该防止用户在同一页面上时重构 bean... 那么为什么我的 @ViewScoped
JSf Controller bean甚至在操作处理程序导致浏览器离开该 View 之前就被多次创建?
谁能给我指出正确的方向?
这是我的代码:
View (域/edit.xhtml)
<h:form prependId="false">
<h:inputText id="descriptionField" value="#{domainEdit.domain.description}" />
<h:commandButton id="saveButton" value="save" action="#{domainEdit.save}" />
</h:form>
ViewScoped Controller (DomainEdit.java)
@Named("domainEdit")
@ViewScoped
public class DomainEdit implements Serializable {
private static final long serialVersionUID = 1L;
protected DomainEdit() {
}
@PostConstruct
protected void init() {
System.out.println("post construct called.");
}
@PreDestroy
public void destroy() {
System.out.println("pre destroy called.");
}
public DomainEntity getDomain() {
System.out.println("displaying domain...");
// some code to return the domain
return domain;
}
public String save() {
System.out.println("saving...");
// some saving code
return "view";
}
}
输出
当我部署它并执行以下操作时,我得到以下输出:
导航到编辑 View (edit.xhtml)
post construct called. displaying domain... pre destroy called.
更改domainDescriptionField输入文本的内容
没有记录
点击“保存”
post construct called. displaying domain... pre destroy called. post construct called. displaying domain... pre destroy called. post construct called. displaying domain... pre destroy called. post construct called. displaying domain... pre destroy called. post construct called. displaying domain... saving domain... pre destroy called.