一千萬個為什麽

搜索

如何使用<p:ajax listener>進行導航

我試圖設置一種方式,當用戶選擇一行時加載另一個頁面。行 return Pages.EDIT_ITEM 被執行,但沒有任何反應,沒有任何異常或錯誤,完全沒有。我甚至可以通過輸入URL來訪問瀏覽器中的 Pages.EDIT_ITEM ,但不知何故,讓後臺bean打開頁面不起作用。

我的數據表:


   
         
   
                                           
       
   

我的支持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 doListItems(Category category) {
        final List 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”);

轉載註明原文: 如何使用<p:ajax listener>進行導航