如何使用 <p:ajax 侦听器进行导航>



我正在尝试设置一个,以便在用户选择一行时加载另一个页面。行return Pages.EDIT_ITEM被执行,但没有任何反应,没有异常或错误,什么都没有。我甚至可以通过键入 URL 在浏览器中访问Pages.EDIT_ITEM,但不知何故,让后备 Bean 打开页面不起作用。

我的数据表:

<p:dataTable id="categoriesTable" value="#{category.items}" var="item" selectionMode="single" rowKey="#{item.id}" >
   <p:commandButton id="toggler" type="button" value="Columns" style="float:right"  />
   <p:ajax event="rowSelect" listener="#{listCategoriesController.onRowDblClick}"  />      
   <p:columnToggler datasource="categoriesTable" trigger="toggler" />
   <p:column headerText="#{msg['addItem.internal_id']}" sortBy="#{item.internalId}" filterBy="#{item.internalId}" filterMatchMode="contains" >                                        
       <h:outputText value="#{item.internalId}" />
   </p:column>
</p:dataTable>

我的背豆:

    package de.rcwgmbh.inventory.controller;
import de.rcwgmbh.inventory.data.CategoryProducer;
import de.rcwgmbh.inventory.data.ItemProducer;
import de.rcwgmbh.inventory.model.Category;
import de.rcwgmbh.inventory.model.Item;
import de.rcwgmbh.inventory.services.ItemService;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.event.UnselectEvent;
/**
 *
 * @author jalexakis
 */
@ViewScoped
@Named
public class ListCategoriesController implements Serializable {
    private static final long serialVersionUID = -2347239478324L;  
    @Inject
    private EntityManager entityManager;
    @Inject
    private CategoryProducer categoryProducer;
    @Inject
    private ItemService itemService;
    @Inject
    private ItemProducer itemProducer;

     public List<Item> doListItems(Category category) {
        final List<Item> items = itemService.getItemsList(category);
        category.setItems(items);
        categoryProducer.setSelectedCategory(category);
        return items;
    }
    public String doAddCategory() {
        categoryProducer.prepareAddCategory();
        return Pages.LIST_CATEGORIES;
    }
    public String doEditCategory(Category category) {
        categoryProducer.prepareEditCategory(category);
        return Pages.LIST_CATEGORIES;
    }
    public String doEditItem(){
        itemProducer.prepareEditItem(itemProducer.getSelectedItem());
        return Pages.EDIT_ITEM;
    }

    public void onTabChange(TabChangeEvent event) {
        String categoryName = event.getTab().getTitle();
        Category category = entityManager.find(Category.class, categoryName);
        categoryProducer.setSelectedCategory(category);
        ResourceBundle bundle = ResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        String value = bundle.getString("listCategories.category_selected");
        FacesMessage msg = new FacesMessage(value, event.getTab().getTitle());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }       

    public void onRowDblClick(SelectEvent event){
        itemProducer.setSelectedItem((Item)event.getObject());
        doEditItem();
    }
}

您不会在此代码中执行重定向。
Ajax 侦听器调用与 CommandButtonaction 不同,如果您返回String则不会发生任何事情。
如果要重定向到另一个页面,可以在ajax调用中执行此操作:FacesContext.getCurrentInstance().getExternalContext().redirect("otherpage.xhtml");

这是另一种方法,它通过 JSF 导航处理 API,允许您根据需要应用显式导航或通过导航规则。此代码可在Primefaces论坛中找到,为方便起见,此处引用了此代码。

    ConfigurableNavigationHandler configurableNavigationHandler =
    (ConfigurableNavigationHandler) FacesContext.getCurrentInstance()
        .getApplication().getNavigationHandler();
    configurableNavigationHandler.performNavigation("otherpage?faces-redirect=true);

相关内容

  • 没有找到相关文章

最新更新