ViewScoped就像RequestScoped一样工作-为什么? [英] ViewScoped works like RequestScoped - why?

查看:70
本文介绍了ViewScoped就像RequestScoped一样工作-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个ViewScoped Managed-Bean,每次在Web浏览器中刷新页面时,似乎都重新创建了Managed-Bean,article为空,它加载了一个新的article-object,依此类推.对我来说,它的行为与RequestScoped相同.

I wrote a ViewScoped Managed-Bean and every time I refresh the page in my webbrowser, the Managed-Bean seems to be recreated, article is null, it loads a new article-object and so on. For me it looks like the same behaviour as RequestScoped.

我将Eclipse IDE用于Java EE开发人员,最新的JDK,Apache Tomcat 7.0.8和Mojarra 2.0.3.

I use Eclipse IDE for Java EE Developers, the newest JDK, Apache Tomcat 7.0.8 and Mojarra 2.0.3.

怎么了?

Managed-Bean:

Managed-Bean:

...
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:

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>

推荐答案

这是预期的行为.当您通过刷新浏览器中的页面来触发全新的HTTP GET请求时,可以肯定会重新创建该页面.否则,它将像会话作用域的bean一样起作用,并使视图作用域无用(请考虑不同浏览器选项卡中的新GET请求!).但是,当您调用任何ajax请求或在同一视图中提交表单时,将不会重新创建.每次都将重新创建范围为一个请求.这是视图作用域bean的核心点/优势.

That's expected behaviour. Surely it will be recreated when you fire brand new HTTP GET requests by refreshing the page in the browser. Otherwise it would act like a session scoped bean and it would make the view scope useless (think about new GET requests in different browser tabs!). It will however not be recreated when you invoke any ajax requests or submit the form in the same view. A request scoped one would be recreated everytime. That's the core point/advantage of a view scoped bean.

这篇关于ViewScoped就像RequestScoped一样工作-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆