Rya*_*tts 2 jsf ejb java-ee view-scope
我以为@ViewScoped
当用户在同一页面上时,我认为应该阻止bean被重建...那么为什么我的@ViewScoped
JSf控制器bean甚至在操作处理程序导致浏览器离开该视图之前被多次创建?
任何人都能指出我在正确的方向吗?
这是我的代码:
<h:form prependId="false">
<h:inputText id="descriptionField" value="#{domainEdit.domain.description}" />
<h:commandButton id="saveButton" value="save" action="#{domainEdit.save}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
@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";
}
}
Run Code Online (Sandbox Code Playgroud)
我在部署它时执行以下输出并执行以下操作:
导航到编辑视图(edit.xhtml)
Run Code Online (Sandbox Code Playgroud)post construct called. displaying domain... pre destroy called.
更改domainDescriptionField输入文本的内容
没有记录
点击"保存"
Run Code Online (Sandbox Code Playgroud)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.
除非你使用的是JSF 2.2(目前还没有出现)或MyFaces CODI(我预计你会明确提到它),@ViewScoped
否则它在CDI中不起作用.这也非常符合您的问题症状.
通过JSF而不是CDI管理bean.更换@Named("domainEdit")
由@ManagedBean
从javax.faces.bean
包.或者,安装MyFaces CODI以将JSF连接@ViewScoped
到CDI.
归档时间: |
|
查看次数: |
3976 次 |
最近记录: |