0

我有前端 JSF/RichFaces、中间层 Spring 和 Hibernate for DB 的 Web 应用程序。当我部署应用程序并从我的系统和我的朋友访问中访问时,我的朋友可以继续我所做的任何操作(对象表现为单例/应用程序上下文)。下面是我的会话类 web.xml、faces-config.xml 和 spring.xml 的代码。我想由于同样的原因,我相信 viewscoped bean 也没有达到我的预期。在这种情况下有人可以帮忙吗?

package org.kp.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class OliHolder implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Map<String, ArrayList<OliData>> oliTypesHolder = new HashMap<String, ArrayList<OliData>>();


    public void addOli(String oliType) {
        OliData oliDetails = new OliData();
        oliDetails.setOliName(oliType);
        oliDetails.initData(oliType);
        ArrayList<OliData> tempHolder = null;
        if(!oliTypesHolder.containsKey(oliType))
        {
            tempHolder = new ArrayList<OliData>();
            tempHolder.add(oliDetails);
            oliTypesHolder.put(oliType, tempHolder);
        }else{
            tempHolder = oliTypesHolder.get(oliType);
            tempHolder.add(oliDetails);
        }
    }

    public void deleOli(int num, String oliType) {
        ArrayList<OliData> list = oliTypesHolder.get(oliType);
        if(null != list && list.size() > 0){
            if((list.size()>num)){  
                list.remove(num);
            }
        }
    }

    public void resetOliHolder(){
        oliTypesHolder.clear();
    }

    public List<OliData> getOlisByType(String oliType){
        ArrayList<OliData> tempHolder = oliTypesHolder.get(oliType);
        return tempHolder;
    }

    public void resetCurrentType(String selectedOli) {
         oliTypesHolder.remove(selectedOli);
    }

    public Map<String, ArrayList<OliData>> getOliTypesHolder() {
        return oliTypesHolder;
    }

}

人脸配置.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
    version="2.1">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

web.xml

<?xml version="1.0"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>EBMMaker</display-name>
    <!-- <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>org.richfaces.skin</param-name>
        <param-value>ruby</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfig.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
</web-app>

春天.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        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.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

        <context:annotation-config />
        <context:component-scan base-package="org.kp"></context:component-scan>

        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/ebm" />
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="örg.kp.db.model" />
            <property name="configLocation">
                <value>
                    classpath:/org/kp/db/hibernate.cfg.xml
                </value>
            </property>
        </bean>

</beans>

这是我正在尝试访问用于从数据库获取数据的 bbOli Bean 的一类由于我是手动创建对象,因此我正在使用应用程序上下文访问 bbOli Bean。我不确定 OLIData 对象的范围。

package org.kp.business;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.kp.db.model.TblEBMFieldDetails;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class OliData {

    BusinessOli bbOli;

    private String oliName;

    public OliData() {
        ApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext((ServletContext) FacesContext
                        .getCurrentInstance().getExternalContext().getContext());
        bbOli = (BusinessOli) ctx.getBean("bbOli");
    }

    private String commentText;


    public String getCommentText() {
        return commentText;
    }

    public void setCommentText(String commentText) {
        this.commentText = commentText;
    }

    public String getOliName() {
        return oliName;
    }

    public void setOliName(String oliName) {
        this.oliName = oliName;
    }

    private List<TblEBMFieldDetails> dbEBMData;
    private List<EBMDataForView> renderEBMData;

    public List<EBMDataForView> getRenderEBMData() {
        return renderEBMData;
    }

    public void setRenderEBMData(List<EBMDataForView> renderEBMData) {
        this.renderEBMData = renderEBMData;
    }

    public List<TblEBMFieldDetails> getDbEBMData() {
        return dbEBMData;
    }

    public void setDbEBMData(List<TblEBMFieldDetails> dbEBMData) {
        this.dbEBMData = dbEBMData;
    }

    public void initData(String oliName) {
        this.dbEBMData = bbOli.getOliTypeSpfcData(oliName);
        renderEBMData = new ArrayList<EBMDataForView>();
        for (TblEBMFieldDetails dt : dbEBMData) {
            EBMDataForView data = new EBMDataForView();
            data.setCmplxFlg(dt.isCollectionFlg());
            if (dt.isCollectionFlg() && null != dt.getValues()) {
                data.setFieldCollectionValue(dt.getValues().split(","));
                System.out.println("Hey I'm: "+data.getFieldCollectionValue().length);
            }

            data.setFieldDefaultValue(dt.getDefaultValue());
            data.setFieldName(dt.getFieldName());
            data.setRequiredFld(dt.isReqFlg());
            renderEBMData.add(data);
        }
    }

}

改变后

import org.springframework.beans.factory.annotation.Autowired;

@ManagedBean(name = "bbOliHolder")
@ViewScoped
public class OliHolderBacking implements Serializable{


    private static final long serialVersionUID = 1L;

    private String selectedTab;

    @Autowired
    BusinessOli bbOli;

    public BusinessOli getBbOli() {
        return bbOli;
    }


    public void setBbOli(BusinessOli bbOli) {
        this.bbOli = bbOli;
    }

    @Autowired
    OliHolder oliHolder;

    private String[] allOliTypes = null;


    @PostConstruct
    public void init() {

        allOliTypes = getBbOli().getAllOlisFromDB();
        olis = new ArrayList<OliData>();
    }

}

服务层 Bean

import java.util.List;

import org.kp.db.dao.EBMDetailDao;
import org.kp.db.dao.impl.OlisImpl;
import org.kp.db.model.TblEBMFieldDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service("bbOli")
@Scope("prototype")
public class BusinessOli{

    @Autowired
    OlisImpl olisImpl;

    @Autowired
    EBMDetailDao ebmData;

    public String[] getAllOlisFromDB() {

        String[] str = olisImpl.getAllOlis();
        return str;
    }
}

然而,BusinessOli 中的自动装配在更改之前就已经开始工作了

4

2 回答 2

1

据我所知,Spring 不支持 Java EE 6 (JSR 299) 中引入的 CDI 注释,因此您应该使用 spring 注释来定义 bean 的范围,在您的情况下是:

import org.springframework.context.annotation.Scope;

@Scope("session")

在您的情况下,Spring 使用了默认范围,这确实是单例。

于 2013-03-06T14:32:18.943 回答
0

如果同时使用 Spring 和 JSF,那么使用 Spring beans 而不是 JSF Managed bean 会很有好处。Spring bean 提供了 JSF 托管 bean 的几乎所有功能,并且在此之上添加了许多有用的 Spring 特性(AOP、代理、编织、MessageSources 等)。

如果您决定使用 Spring bean,则必须使用 Spring Scopes,如下所示WebApplicationContext

import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext

@Scope(WebApplicationContext.SCOPE_SESSION)

查看范围

有一个问题。如您所见,SCOPE_VIEWWebApplicationContext. Spring 尚不支持 JSF 2 视图范围。现在,您必须提供自己的视图范围实现。这并不难:

于 2013-03-06T16:05:05.357 回答