0

我有一个使用 JSF 2.0、mojarra 2.2.4、Tomcat 7 和 Primefaces 3.5 的项目。我们还使用 Spring 和 Hibernate。

我面临一个关于带@PostContruct注释的方法的问题。它应该只在创建页面时调用一次,而是调用超过 10 次。当我按下 a 时也会调用<p:commandButton/> 它 SessionScoped Beans 不会发生多个调用,只有我的自定义 ViewScoped bean 才会发生。

这是自定义 ViewScope 实现:

视野范围

public class ViewScope implements Scope {

@Override
@SuppressWarnings("rawtypes")
public Object get(String name, ObjectFactory objectFactory) {       
    if (FacesContext.getCurrentInstance().getViewRoot() != null) {
        Map<String, Object> viewMap = FacesContext.getCurrentInstance()
            .getViewRoot().getViewMap();

        if (viewMap.containsKey(name)) {                
            Object bean = viewMap.get(name);

            // restore a transient autowired beans after re-serialization bean
            WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
            AutowireCapableBeanFactory autowireFactory = webAppContext.getAutowireCapableBeanFactory();
            if (webAppContext.containsBean(name)) {                 
                // Reconfigure restored bean instance.
                bean = autowireFactory.configureBean(bean, name);
            }
            // end restore

        return bean;
        } else {
            Object object = objectFactory.getObject();
            viewMap.put(name, object);
            return object;
        }
    }else{
        return null;
    }   
}

@Override
public Object remove(String name) {

    return FacesContext.getCurrentInstance().getViewRoot().getViewMap()
            .remove(name);
}

public String getConversationId() {
    return null;
}

@Override
public void registerDestructionCallback(String name, Runnable callback) {
}

public Object resolveContextualObject(String key) {
    return null;
}

 public void clearBean() { FacesContext.getCurrentInstance()
        .getViewRoot().getViewMap().clear();
}

}

豆子

@SuppressWarnings("serial")
@Component("grupoFormBean")
@Scope("view")
public class GrupoFormBean implements Serializable {
Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(FacesContext.getCurrentInstance().getViewRoot());

@PostConstruct
public void init(){
    System.out.println("=========== entering PostConstruct =============");
    this.comboSistema = sistemaService.findAll();
    System.out.println("=========== leaving PostConstruct =============");
}   

public void prepara(){
    if(!FacesContext.getCurrentInstance().isPostback()){
        if(grupoId==null){              
            return;
        }else{              
            this.alterar = true;
            setGrupo(grupoService.findById(grupoId));
            prepareAlterar();               
        }
    }
}
//implementation of others components
//getters and setters

}

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">
    <ui:composition template="../WEB-INF/templates/page.xhtml"
      xmlns="http://www.w3.org/1999/xhtml"      
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:define name="content">

    <f:metadata>
    <f:viewParam name="idgrupo" id="idgrupo" value="#{grupoFormBean.grupoId}"/>
        <f:event type="preRenderView" listener="#{grupoFormBean.prepara}">
        </f:event>
    </f:metadata>

        <div class="titulo-pagina">

            <h2>Cadastro de Grupo</h2>

        </div>

        <h:form id="formulario" styleClass="formularios">

    //... page stuff

    </h:form>

    </ui:define>
</ui:composition>

这是我第一次进入页面时的日志:

=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as    SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as     SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- View scope -->
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="view">
                <bean class="mypackage.util.ViewScope"/>
            </entry>
        </map>
    </property>
</bean>

</beans>
4

0 回答 0