JSF 2登录用户之间的托管bean并发访问

时间:2016-08-29 17:50:11

标签: spring jsf jsf-2 managed-bean

展示我的问题的基本示例:

  1. 两个用户A和B从不同的浏览器连接到JSF Web应用程序,欢迎页面显示他们的文档和搜索面板。
  2. 他们都希望使用单词或句子搜索文档。
  3. (例如)A想要搜索单词" sheet"而且B想要搜索“#34; virus"”这个词。
  4. 他们在表格输入中写下这个单词并同时提交表格。
  5. A得到她的搜索结果,而B突然得到与A相同的结果,并且他没有键入&#34; sheet&#34;。< / LI>

    这是我的managedBean:

    @ManagedBean
    @RequestScoped
    public class DocumentController implements Serializable {
    
    private String motSearch= null;
    private boolean searching = false;
    private boolean rechercheStructure = true;
    private DocumentDao dao; //Dependency injection with Spring
    
    public void onload(Utilisateur user)
    {
        if(!searching)
        {
            motSearch = null;
    
            documentsListForConnectedUser(user);
        }
        else{
            search(user);
        }
    }
    
    public void search(Utilisateur user)
    {
        documentsListForConnectedUser(user);// getting documents list from database
    
        searching = true;
    
        List<Document> list = new ArrayList<>();
        list.addAll(user.getDocuments());
    
        user.setDocuments(new ArrayList<>());
    
        //...
    
        if(motSearch != null && !motSearch.isEmpty())
        {
            LOGGER.info("motSearch : "+motSearch.toLowerCase());
    
            List<Document> docs = user.getDocuments();
    
            for(Document doc : list)
            {
                if(!docs.contains(doc) && (doc.getNom().toLowerCase().contains(motSearch.toLowerCase()) ||
                        doc.getDescription().toLowerCase().contains(motSearch.toLowerCase()) ||
                        doc.getTheme().toLowerCase().contains(motSearch.toLowerCase()) ||
                        doc.getKeywords().contains(motSearch.toLowerCase())))
                {
                    docs.add(doc);
                }
            }
    
            docs = filter(docs);
    
            if(docs.size() == 0)
            {
                user.setDocuments(null);
            }
        }
        else
        {
            user.setDocuments(list);
        }
    
    }
    }
    

    JSF页面ListeDocuments.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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
    <f:metadata>
        <f:viewParam name="searching" value="#{documentController.searching}" />
        <f:viewParam name="structure" value="#{documentController.rechercheStructure}"/>
        <f:viewParam name="q" value="#{documentController.motSearch}"/>
        <f:viewAction action="#{documentController.onload(user)}"/>
    </f:metadata>
    </h:head>
    
    <h:body>
    <h:form id="myForm" prependId="false" >
            <h:inputHidden value="true" id="searching"/>
    
            <div class="center">
                <h:selectOneRadio converter="javax.faces.Boolean"
                                  value="#{documentController.rechercheStructure}" onchange="check()" id="structure">
                    <f:selectItem itemLabel="Recherche structurée" itemValue="true" />
                    <f:selectItem itemLabel="Recherche plein texte" itemValue="false" />
                </h:selectOneRadio>
            </div>
    
            <div class="row">
                <div class="input-field col s12 m7 l8">
                    <p:inputTextarea styleClass="materialize-textarea" id="q" value="#{documentController.motSearch}"/>
                    <label for="q">Mot/phrase à chercher</label>
                </div>
            </div>
    
            <p:commandButton styleClass="btn blue-grey" update=":table"
                             type="submit" value="Chercher"
                             action="ListeDocuments?faces-redirect=true&amp;includeViewParams=true" />
    </h:form>
    
    <p:dataTable id="table" tableStyleClass="hoverable striped" rows="10" paginator="true"
                 value="#{user.documents}" var="doc"
                 paginatorPosition="bottom" paginatorAlwaysVisible="false" styleClass="borderless">
    
        <!--Columns-->
    
    </p:dataTable>
    
    </h:body>
    
    </html>
    

    我的web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    
    <session-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>
    
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    
    <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>/faces/*</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>*.xhtml</url-pattern>
    </servlet-mapping>
    
    <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
    </error-page>
    
    <error-page>
    <error-code>500</error-code>
    <location>/serverError.xhtml</location>
    </error-page>
    
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/serverError.xhtml</location>
    </error-page>
    
    <!-- Add Support for Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>none</param-value>
    </context-param>
    
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    
    </web-app>
    

    faces-config.xml文件:

    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
    

    PS :我正在使用:

    1. Mojarra 2.2.9 JSF实施。
    2. 带有Servlet 3.0的Apache Tomcat 7服务器。
    3. Spring但我只使用它来注入DAOs等接口实现。
    4. Hibernate 4 / JPA as ORM。
    5. 由于我需要将Omnifaces 2.4库用于其转换器,因此我不得不整合CDIweld-servlet-2.3.5 final)并创建一个空的beans.xml(来自BalusC博客的指南),我在我的managedBeans中使用其任何注释。
    6. 我使用的唯一注释是来自@ManagedBean的{​​{1}}和@xxxxScoped,有时是javax.faces.bean
    7. 最后是Primefaces 6.0
    8. 感谢任何帮助!

0 个答案:

没有答案