当用户点击一行时,如何导航到其他页面?

时间:2011-10-05 06:13:51

标签: java jsf-2 richfaces

我希望能够在用户点击行时导航到其他页面 的 问题

  • 没有抛出错误我可以看到selectionListener被执行了预期的行值,但没有任何反应。
  • 这是否是正确的方法,即为此目的使用我的动作监听器。

支持豆就是这个 支持bean

package com.howto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.DataModel;

import org.richfaces.component.UIExtendedDataTable;

@ManagedBean(name = "backingBean")
@SessionScoped
public class BackingBean {
    private List<String> names;
    private DataModel<String> name;
    private String rowValue;
   // private List<String> selection;
    private Collection<String> selection;

    @PostConstruct
    public void init() {
        names = new ArrayList<String>();
        names.add("MyName");
        names.add("YourName");
        names.add("OurName");
        names.add("MamaDidNotGiveOne");
    }

    public void update() {
        System.out.println("Somebody clicked "
                + name);
    }

    public List<String> getNames() {
        return names;
    }

    public void setNames(List<String> names) {
        this.names = names;
    }

    public DataModel<String> getName() {
        return name;
    }

    public void setName(DataModel<String> name) {
        this.name = name;
    }

    public String selectionListener(AjaxBehaviorEvent event) {
        UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
        Object originalKey = dataTable.getRowKey();
      //  selectionItems.clear();
        for (Object selectionKey : selection) {
            dataTable.setRowKey(selectionKey);
            if (dataTable.isRowAvailable()) {
                //selectionItems.add((InventoryItem) dataTable.getRowData());
                rowValue =  (String) dataTable.getRowData();
            }
        }        
        dataTable.setRowKey(originalKey);
        return "success";
    }

    public String gotopage() {
        return "success";
    }

    public Collection<String> getSelection() {
        return selection;
    }

    public void setSelection(Collection<String> selection) {
        this.selection = selection;
    }

    public String getRowValue() {
        return rowValue;
    }

    public void setRowValue(String rowValue) {
        this.rowValue = rowValue;
    }

}

每次用户点击一个行项时,方法selectionListener都会被击中,不会抛出任何错误但没有任何反应 的 XHTML

        <h:form>
            <rich:extendedDataTable
                value="#{backingBean.names}" var="rowItem"
                selection="#{backingBean.selection}" id="table"
                frozenColumns="2" style="height:300px; width:500px;" selectionMode="single">
                <a4j:ajax event="selectionchange" listener="#{backingBean.selectionListener}" />                    
                <rich:column>
                    <f:facet name="header">
                        <h:outputText value="vendor" />
                    </f:facet>
                    <h:outputText value="#{rowItem}" />
                </rich:column>
            </rich:extendedDataTable>
        </h:form>

导航规则

<navigation-rule>
        <from-view-id>*</from-view-id>
         <navigation-case>
            <from-action>#{backingBean.selectionListener}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/dummyfolder/results.xhtml</to-view-id>
        </navigation-case>
</navigation-rule>

使用 Richfaces 4 找不到包含版本的标记。

1 个答案:

答案 0 :(得分:3)

您无法通过从动作侦听器方法返回String来导航。您只能在实际操作方法中执行此操作。您需要将方法返回类型设置回void,并在NavigationHandler#handleNavigation()的帮助下自行完成导航。

public void selectionListener(AjaxBehaviorEvent event) {
    // ...

    FacesContext context = FacesContext.getCurrentInstance();
    context.getApplication().getNavigationHandler().handleNavigation(context, "#{backingBean.selectionListener}", "success");
}