gpt4 book ai didi

java - ViewScoped 的工作方式类似于 RequestScoped - 为什么?

转载 作者:行者123 更新时间:2023-11-29 06:45:49 25 4
gpt4 key购买 nike

我写了一个 ViewScoped Managed-Bean,每次我在我的网络浏览器中刷新页面时,Managed-Bean 似乎被重新创建,文章为空,它加载一个新的文章对象等等。对我来说,它看起来与 RequestScoped 的行为相同。

我使用面向 Java EE 开发人员的 Eclipse IDE、最新的 JDK、Apache Tomcat 7.0.8 和 Mojarra 2.0.3。

怎么了?

托管 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";
}

}

创建文章.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 请求时,它肯定会被重新创建。否则它会像 session 范围的 bean 一样工作,并且会使 View 范围变得无用(想想不同浏览器选项卡中的新 GET 请求!)。但是,当您调用任何 ajax 请求或在同一 View 中提交表单时,它不会被重新创建。每次都会重新创建一个范围内的请求。这是 View 作用域 bean 的核心点/优势。

关于java - ViewScoped 的工作方式类似于 RequestScoped - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5220407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com