如何使用<p:ajax listener =“”> </p:ajax>进行导航

时间:2015-03-17 10:52:09

标签: jsf primefaces navigation

我正在尝试以用户选择行时加载另一个页面的方式设置。行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>

我的支持bean:

    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();
    }

}

2 个答案:

答案 0 :(得分:3)

您不会在此代码中进行重定向 Ajax监听器调用与action的{​​{1}}不同,如果您返回CommandButton,则不会发生任何事情。
如果您想重定向到另一个页面,可以在ajax调用中执行此操作:String

答案 1 :(得分:0)

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

    ConfigurableNavigationHandler configurableNavigationHandler =
    (ConfigurableNavigationHandler) FacesContext.getCurrentInstance()
        .getApplication().getNavigationHandler();

    configurableNavigationHandler.performNavigation("otherpage?faces-redirect=true);