Java ViewScope ManagedBean在非ajax post后保持活动状态
我正在使用PrimeFaces4.0、Spring、Spring Security和Hibernate。我不知道为什么在ajax=“false”命令按钮中发布帖子后,ViewScope bean会返回到表单中所有旧值没有被摧毁。在其他ViewScoped bean中,它工作正常,但这并非如此。我试图解决这个问题已经有一段时间了,但我无法让它工作 表单相当大,它将简化以使其清晰Java ViewScope ManagedBean在非ajax post后保持活动状态,java,spring,jsf,primefaces,view-scope,Java,Spring,Jsf,Primefaces,View Scope,我正在使用PrimeFaces4.0、Spring、Spring Security和Hibernate。我不知道为什么在ajax=“false”命令按钮中发布帖子后,ViewScope bean会返回到表单中所有旧值没有被摧毁。在其他ViewScoped bean中,它工作正常,但这并非如此。我试图解决这个问题已经有一段时间了,但我无法让它工作 表单相当大,它将简化以使其清晰 <h:form id="anadirForm"> <p:panel id="panel" hea
<h:form id="anadirForm">
<p:panel id="panel" header="Añadir factura">
<h:panelGrid id="grid" columns="2" style="margin-bottom:10px">
<f:facet name="header">
<p:messages id="messages" autoUpdate="true" />
</f:facet>
<h:outputLabel for="justificante" value="Justificante: *" />
<p:inputText id="justificante"
value="#{facturaBean.factura.justificante}" required="false"
>
<p:ajax event="change" listener="#{facturaBean.comprobarJustificante}"/>
</p:inputText>
....
</h:panelGrid>
</p:panel>
<p:dataTable style="width: 780px;" id="lineasFacturas" var="facturaLinea" value="#{facturaBean.factura.lineas}">
<p:column>
<p:inputText value="#{facturaLinea.contrapartida}" />
</p:column>
<p:column style="width: 90px;">
<p:inputText style="width: 85px;" value="#{facturaLinea.base}" >
<p:ajax event="change" process="lineasFacturas" update="lineasFacturas" listener="#{facturaBean.sumarBases}"/>
</p:inputText>
</p:column>
.....
</p:dataTable>
<p:commandButton value="Guardar" process="@all" ajax="false" action="#{facturaBean.saveFactura}" >
</h:form>
....
.....
Bean
@ManagedBean
@ViewScoped
public class FacturaBean implements Serializable{
private List<FacturaLinea> facturaLineas;
private Factura factura;
private FacturaLinea facturaLin;
priv
....
@PostConstruct
public void init(){
facturaLineas = new ArrayList<FacturaLinea>();
factura = new Factura();
factura.n
....
}
....
public void saveFactura(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
DateFormat hourFormat = new SimpleDateFormat("HH:mm:ss");
Date hour = new Date();
String fechaFactura = null;
String fechaPunteo = sdf.format(hour);
String horaPunteo = hourFormat.format(hour);
try {
Date dat = dateFormat.parse(factura.getFechaFactura());
fechaFactura = sdf.format(dat);
}catch (ParseException e){
}
factura.setFechaFactura(fechaFactura);
factura.setFechaPunteo(fechaPunteo);
factura.setHoraPunteo(horaPunteo);
factura.setAgente(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
calcularImporte();
dao.save(factura);
}
....
public void sumarBases(){
Integer suma = 0;
for(FacturaLinea fl :factura.getLineas()){
suma += fl.getBase();
}
factura.setSumaBases(suma);
calcularImporte();
}
public void comprobarJustificante(){
for(Factura f : lista){
if(factura.getJustificante() == f.getJustificante()){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Justificante en uso"));
break;
}
}
}
@ManagedBean
@视域
公共类FacturaBean实现了可序列化{
私人名单;
私人财产;
私人FacturaLinea facturaLin;
普里夫
....
@施工后
公共void init(){
facturaLineas=新的ArrayList();
factura=新factura();
事实
....
}
....
public void saveFactura(){
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
DateFormat DateFormat=新的简化格式(“EEE-MMM-dd-HH:mm:ss Z-yyyy”,Locale.ENGLISH);
DateFormat hourFormat=新的SimpleDateFormat(“HH:mm:ss”);
日期小时=新日期();
字符串fechaFactura=null;
字符串fechaPunteo=sdf.format(小时);
字符串horaPunteo=hourFormat.format(小时);
试一试{
Date dat=dateFormat.parse(factura.getFechaFactura());
fechaFactura=sdf.format(dat);
}捕获(解析异常){
}
长毛象(长毛象);
factura.setFechaPunteo(fechaPunteo);
setHoraPunteo(horaPunteo);
setAgente(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
calcularImporte();
保存(factura);
}
....
公共无效sumarBases(){
整数suma=0;
for(FacturaLinea fl:factura.getLineas()){
suma+=fl.getBase();
}
factura.setSumaBases(suma);
calcularImporte();
}
公共无效comprobarJustificante(){
对于(事实f:lista){
如果(factura.getRighticante()==f.getRighticante()){
FacesContext.getCurrentInstance().addMessage(空,新的FacesMessage(“justifCante en uso”);
打破
}
}
}
我尝试在saveFactura方法中返回一个字符串,因为我在BalusC博客上使用了它,其中写道:
“@ViewScoped:只要您在浏览器窗口/选项卡中与相同的JSF视图交互,此范围内的bean就会存在。它会在HTTP请求时创建,并在您回发到其他视图后被销毁……您需要从action(listener)方法返回null或void以保持bean的活动。”
但是豆子没有被任何方式破坏,我不知道现在该怎么办
很抱歉出现语法或拼写错误。您希望发生什么?我认为您的体验是正常的。如果您希望使用重置字段显示同一页面,您可以在saveFactura()中调用init()last。或者您可以将bean RequestScope设置为。如果您想导航到另一个页面,请使用按钮上的action而不是actionListener,并从该方法返回导航字符串。据我所知,从actionListener调用字符串时返回字符串没有任何意义,事实上是不允许的。您在消息中是对的itten actionListener,但我把它放在测试中,忘了更改,我知道它必须是一个操作,但作为操作它不起作用。很抱歉,我编辑了这篇文章。谢谢;)你是对的,你不能让它成为RequestScope,因为Ajax不起作用。所以我相信你的选项是手动重置bean,或者通过操作调用saveFactura()(不是actionListener)并返回重定向到此处提到的同一页面:返回字符串时是否重定向到新视图(URL更改)?如果是,则可能是FacturaBean被重新初始化。您可以通过在init()方法中添加print语句来测试它