为啥此 ajax 操作不适用于 requestScope?

Posted

技术标签:

【中文标题】为啥此 ajax 操作不适用于 requestScope?【英文标题】:Why does this ajax action not work with requestScope?为什么此 ajax 操作不适用于 requestScope? 【发布时间】:2011-06-24 22:44:57 【问题描述】:
package com.atlanticpkg.view.beans;

import com.atlanticpkg.controller.ejb.ContactsFacade;
import com.atlanticpkg.model.entities.Contacts;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named(value = "contactsBean")
@SessionScoped
public class ContactsBean implements Serializable 

    @EJB
    ContactsFacade contactsEJB;
    private List<Contacts> contacts = new ArrayList<Contacts>();
    private int page = 0;
    private int contactsRecords;
    private boolean previousDisabled = false;
    private int firstItem = 0;
    private int batchSize = 5;

    public ContactsBean() 
    

    @PostConstruct
    public void onLoad() 

        contactsRecords = contactsEJB.count();
        updateContactsList();
    

    public void updateContactsList() 

        if (firstItem <= 0) 
            previousDisabled = true;
        
        if (firstItem > 0) 
            previousDisabled = false;
        

        contacts = contactsEJB.findRange(firstItem, batchSize);
    

    public void next() 

        if (firstItem + batchSize < contactsRecords) 
            firstItem += batchSize;
        

        updateContactsList();
    

    public void previous() 

        if (firstItem > 0) 
            firstItem -= batchSize;
        

        updateContactsList();
    

    /**
     * @return the contacts
     */
    public List<Contacts> getContacts() 
        return contacts;
    

    /**
     * @param contacts the contacts to set
     */
    public void setContacts(List<Contacts> contacts) 
        this.contacts = contacts;
    

    /**
     * @return the contactsRecords
     */
    public int getContactsRecords() 
        return contactsRecords;
    

    /**
     * @param contactsRecords the contactsRecords to set
     */
    public void setContactsRecords(int contactsRecords) 
        this.contactsRecords = contactsRecords;
    

    /**
     * @return the page
     */
    public int getPage() 
        return page;
    

    /**
     * @return the previousDisabled
     */
    public boolean isPreviousDisabled() 
        return previousDisabled;
    

    /**
     * @param previousDisabled the previousDisabled to set
     */
    public void setPreviousDisabled(boolean previousDisabled) 
        this.previousDisabled = previousDisabled;
    

    /**
     * @return the firstItem
     */
    public int getFirstItem() 
        return firstItem;
    

    /**
     * @param firstItem the firstItem to set
     */
    public void setFirstItem(int firstItem) 
        this.firstItem = firstItem;
    

    /**
     * @return the batchSize
     */
    public int getBatchSize() 
        return batchSize;
    

    /**
     * @param batchSize the batchSize to set
     */
    public void setBatchSize(int batchSize) 
        this.batchSize = batchSize;
    

index.xhtml

  <h:form>

                <h:panelGroup id="contactsPanel">
                    <h:dataTable id="contactsTable" value="#contactsBean.contacts" var="contacts">

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Name"/>
                            </f:facet>
                            <h:outputText value="#contacts.name"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Street"/>
                            </f:facet>
                            <h:outputText value="#contacts.street"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="City"/>
                            </f:facet>
                            <h:outputText value="#contacts.city"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="State"/>
                            </f:facet>
                            <h:outputText value="#contacts.state"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Zip"/>
                            </f:facet>
                            <h:outputText value="#contacts.zip"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Country"/>
                            </f:facet>
                            <h:outputText value="#contacts.country"/>
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Sent?"/>
                            </f:facet>
                            <h:selectBooleanCheckbox value="#contacts.sent" />
                        </h:column>
                    </h:dataTable>


                    <h:outputText value="#contactsBean.contactsRecords" />

                    <h:commandLink disabled="#contactsBean.previousDisabled" value="&lt; previous" action="#contactsBean.previous">
                        <f:ajax execute="@form" render="contactsPanel"/>
                    </h:commandLink>

                    <h:commandLink value="next &gt;" action="#contactsBean.next">
                        <f:ajax execute="@form" render="contactsPanel"/>
                    </h:commandLink>

                </h:panelGroup>

            </h:form>

我不确定为什么这不起作用。从技术上讲,页面没有刷新,所以我应该能够使用请求范围对吗?谢谢。

【问题讨论】:

【参考方案1】:

ViewScoped 可能更合适一点,见 Balusc 的一篇文章:http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html

【讨论】:

【参考方案2】:

RequestScope 表示每次向其发送请求时都会实例化 bean。这包括一个 ajax 请求。

【讨论】:

以上是关于为啥此 ajax 操作不适用于 requestScope?的主要内容,如果未能解决你的问题,请参考以下文章

Ajax 加载面板适用于 FF,但不适用于 Chrome。知道为啥吗?

为啥 Firestore 可编码支持不适用于此示例

为啥 background-size:100% 100%;不适用于此 SVG?

为啥此代码不适用于 mcp3008? (来自 adafruit 官方网站)

为啥“重启”操作不适用于 crontab?

为啥 jQuery 选择器适用于 Chrome,但不适用于 Safari?