2011-03-07 3 views
1

ViewScoped Managed-Bean을 작성했고 웹 브라우저에서 페이지를 새로 고칠 때마다 Managed Bean이 다시 생성 된 것처럼 보입니다. 기사가 null이고 새 article-object를로드하는 등의 문제가있었습니다. 내게 그것은 RequestScoped와 같은 행동을하는 것처럼 보입니다.ViewScoped는 RequestScoped처럼 작동합니다 - 왜?

Java EE 개발자를위한 Eclipse IDE, 최신 JDK, Apache Tomcat 7.0.8 및 Mojarra 2.0.3을 사용합니다.

무엇이 잘못 되었나요?

관리 - 콩 :

... 
import javax.annotation.PostConstruct; 
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext; 
... 
@ManagedBean 
@ViewScoped 
public class CreateArticle { 

    @ManagedProperty(value = "#{index.facade}") 
    private PersistenceFacade facade; 
    private Article article; 
    private Vector<ArtCategory> artcat; 

    public CreateArticle() { 
     artcat = ArtCategory.listArtCat(); 
    } 

    @PostConstruct 
    public void postCreateArticle() { 
     if (article == null) { 
      try { 
       article = facade.createArticle(); 
      } catch (DAOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void setFacade(PersistenceFacade facade) { 
     this.facade = facade; 
    } 

    public Vector<ArtCategory> getArtcat() { 
     return artcat; 
    } 

    public Article getArticle() { 
     return article; 
    } 

    public String save() { 
     try { 
      facade.save(article); 
      facade.commit(); 
     } catch (DAOException e) { 
      e.printStackTrace(); 
     } 
     FacesMessage message = new FacesMessage(
       "Successful!"); 
     FacesContext.getCurrentInstance().addMessage(null, message); 
     return "/testpage.xhtml"; 
    } 

} 

createArticle.xhtml : 예상 된 동작 것

<?xml version="1.0" encoding="UTF-8"?> 
<!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:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html"> 
<h:head> 
    <title>Create article</title> 
</h:head> 
<h:body> 
    <h1> 
     <h:outputText value="System" /> 
    </h1> 
    <h2> 
     <h:outputText value="Test1" /> 
    </h2> 
    <h3> 
     <h:outputText value="Test2" /> 
    </h3> 
    <h:form> 
     <h:panelGrid columns="3"> 
      <h:outputLabel for="artname">Articlename</h:outputLabel> 
      <h:inputText id="artname" value="#{createArticle.article.artname}" 
       required="true"> 
       <f:ajax event="blur" render="artnameMessage" /> 
      </h:inputText> 
      <h:message id="artnameMessage" for="artname" /> 

      <h:outputLabel for="briefdesc">Brief description</h:outputLabel> 
      <h:inputTextarea id="briefdesc" 
       value="#{createArticle.article.briefdesc}" required="false"> 
       <f:ajax event="blur" render="briefdescMessage" /> 
      </h:inputTextarea> 
      <h:message id="briefdescMessage" for="briefdesc" /> 

      <h:outputLabel for="price">Price</h:outputLabel> 
      <h:inputText id="price" value="#{createArticle.article.price}" 
       required="true"> 
       <f:ajax event="blur" render="priceMessage" /> 
      </h:inputText> 
      <h:message id="priceMessage" for="price" /> 

      <h:outputLabel for="selectartcat">Article Category</h:outputLabel> 
      <h:selectOneMenu id="selectartcat" 
       value="#{createArticle.article.artcatnr}" required="true"> 
       <f:selectItems value="#{createArticle.artcat}" var="artcat" 
        itemLabel="#{artcat.name}" itemValue="#{artcat.artcatnr}" /> 
       <f:ajax event="blur" render="selectartcatMessage" /> 
      </h:selectOneMenu> 
      <h:message id="selectartcatMessage" for="selectartcat" /> 

      <h:panelGroup /> 
      <h:commandButton value="Save" 
       action="#{createArticle.save}"> 
       <f:ajax execute="@form" render="@form" /> 
      </h:commandButton> 
      <h:messages globalOnly="true" layout="table" /> 
     </h:panelGrid> 
    </h:form> 
</h:body> 
</html> 

답변

5

. 확실히 브라우저에서 페이지를 새로 고침하여 새로운 HTTP GET 요청을 실행하면 다시 만들어집니다. 그렇지 않으면 그것은 세션 범위 bean처럼 행동 할 것이고 뷰 범위를 쓸모 없게 만들 것입니다 (다른 브라우저 탭에서 새로운 GET 요청에 대해 생각하십시오!). 그러나 Ajax 요청을 호출하거나 동일한보기에서 양식을 제출할 때 이 아닌이 다시 작성됩니다. 범위가 지정된 요청은 매번 다시 작성됩니다. 이것은 뷰 범위 bean의 핵심 포인트/장점입니다.

+0

... 나는 언제나 **보기 **가 ** 같은 ** 페이지 **로 정의되어 있다고 생각했습니다! 나는 xyz.xhtml (xyz.jsf) JSF 용어로 view (viewParams, view ..., 무엇이든)를 의미한다. 뼈를 혼란스럽게합니다. 귀하의 게시물을 매일 더 잘 읽고 있습니다. – Kawu

+2

@ 카와 : 예, 일부 용어는 모호함으로 가득합니다. 그것들을 해석하는 방법은 관점에 달려있다;) 더 간단하게 말하자면 뷰 범위 bean은 POST 요청을 제출하는 한 계속 살아있다. GET 요청을 보내면 새로운보기 범위가 생성됩니다. – BalusC

+0

새로 고침을하면 더 이상 같은 페이지에 있지 않습니다! 현재 탭을 새로 고치거나 페이지를 새로 열었을 때 서버가 말할 수있는 방법이 전혀 없습니다. –

0

또한 SessionScoped 빈과 마찬가지로 ViewScoped Bean에 Serializable을 구현해야합니다.

관련 문제