当用户单击一行时,如何导航到不同的页面?

发布于 2024-12-08 09:28:43 字数 3977 浏览 0 评论 0原文

我希望能够在用户单击行时导航到不同的页面。
问题

  • 没有抛出错误我可以看到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找不到带有版本的标签。

I want to be able to navigate to a different page when user clicks are row.
Problems

  • No error is thrown I can see the selectionListener getting executed with expected row value but nothing happens.
  • Is this the right way of doing it i.e. using my action listener for this purpose.

Backing bean is this.

Backing 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;
    }

}

Method selectionListenerdoes get hit every time user clicks a row item no error is thrown but nothing happens.
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

<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>

Using Richfaces 4 could not find a tag with version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热血少△年 2024-12-15 09:28:43

您无法通过从操作侦听器方法返回String来进行导航。你只能用实际的行动方法来做到这一点。您需要将方法返回类型设置回 void 并在 NavigationHandler#handleNavigation()

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

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

You can't navigate by returning String from an action listener method. You can only do this in real action methods. You need to set the method return type back to void and take the navigation in your own hands with help of NavigationHandler#handleNavigation().

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

    FacesContext context = FacesContext.getCurrentInstance();
    context.getApplication().getNavigationHandler().handleNavigation(context, "#{backingBean.selectionListener}", "success");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文