我注意到有不同的bean范围,如:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
Run Code Online (Sandbox Code Playgroud)
每个人的目的是什么?如何为我的bean选择合适的范围?
有时,使用时<h:commandLink>,<h:commandButton>或者<f:ajax>,在action,actionListener或listener与标签相关的方法根本不被调用.或者,bean属性不会使用提交的UIInput值进行更新.
有什么可能的原因和解决方案?
有人可以解释什么是无范围和目的吗?
假设我有一个豆子
request scope as r1
session scope as s1
application scope a1
Run Code Online (Sandbox Code Playgroud)
并且说我没有将范围bean n1注入到上述每个范围中,然后我发现当实例化其父bean [r1/s1/a1]时,n1将被实例化为每个父bean.
a1中的无范围bean在a1中始终可用,因为a1是appl范围.s1中的无范围bean仅在s1未被销毁之后才可用,并且当再次创建s1时,n1将被实例化并且可用于它.
这是对的吗?
和使用它的目的是什么?只是为了避免创造这样的豆我们自己?
非常感谢
我在JSF 2.0项目中使用SessionScoped bean时遇到了一个非常奇怪的问题.使用Netbeans 6.9.1,Glassfish 3服务器和PrimeFaces 3作为JSF组件库.
这是一些代码:
package com.hia.jsf;
import com.hia.netlabel.jpa.Genre;
import com.hia.netlabel.jpa.Label;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean
@SessionScoped
public class LabelDetailJSF implements Serializable{
@ManagedProperty("#{genreLabelListJSF}")
private GenreLabelListJSF genreLabelListJSF;
private List<Genre> DetailLabelGenreList;
private Label DetailLabel;
/** Creates a new instance of LabelDetailJSF */
public LabelDetailJSF() {
}
@PostConstruct
public void init(){
System.out.print("Running init LabelDetailJSF");
if(genreLabelListJSF.getSelectedLabel()!=null)
{
System.out.print("genreLabelListJSF was not null");
this.DetailLabelGenreList=genreLabelListJSF.getSelectedLabel().getGenreList();
this.DetailLabel= (genreLabelListJSF.getSelectedLabel());
}
if(this.DetailLabelGenreList==null){
System.out.println("Bloody thing became null");
}
}
/**
* …Run Code Online (Sandbox Code Playgroud) 我在JSF 2.2.7和Primefaces 5中打开对话框时遇到问题.我有按钮打开一个对话框,每次单击按钮@PostConstruct方法时都会出现问题.为什么?
我只想调用@PostConstruct一次,但我不想将范围更改为Session(使用@SessionScope注释它完美地工作).
这是我的观点:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="f1">
<p:dialog widgetVar="trainingDialog2" id="d1">
<h:outputText value="#{userViewBean.errorMessage}" />
</p:dialog>
<br />
<p:dataTable id="dt1" value="#{userViewBean.infoList}" var="item">
<p:column>
<p:commandButton id="btn" update=":f1:d1"
oncomplete="PF('trainingDialog2').show()"
styleClass="ui-icon ui-icon-calendar">
<f:setPropertyActionListener value="#{item.id}"
target="#{userViewBean.errorMessage}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的豆子:
package pl.jrola.java.www.vigym.viewcontroller.beans.userview;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean(name = "userViewBean")
@ViewScoped
public class UserViewBean implements Serializable {
private static final long serialVersionUID = …Run Code Online (Sandbox Code Playgroud) jsf ×5
jsf-2 ×2
scope ×2
action ×1
commandlink ×1
java ×1
managed-bean ×1
primefaces ×1
view-scope ×1