JSF导航案例无法导航

时间:2012-10-08 23:20:25

标签: jsf-2 primefaces

当我点击搜索按钮时,我正在尝试让我的index.xhtml带我到另一个页面w /结果。我正在使用primefaces JSF。

我的问题是我无法进入下一页。它调用我的searchController bean findItem方法,但结果页面不会改变。它只停留在index.xhtml页面上。

有人有想法吗?

@ManagedBean(name="sController")
@RequestScoped
public class SController implements Serializable {
    private static final long serialVersionUID = 1L;

    public SController() { }

    public String findItem() {
        System.out.println("findItem called!");

        return "success";
    }

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

    public String getName() {
        return name;
    }
} 

这是我的faces-config.xml。

<?xml version="1.0" encoding="UTF-8"?>
<faces-config 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{sController.findItem}</from-action>
        <from-outcome>success</from-outcome>
        <to-view-id>/items.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
</faces-config>

这是我的index.xhtml。

<f:view>
<h:form>
    <p:panel id="search" header="Search Item">
        <p:panelGrid columns="2" cellpadding="5">

            <h:outputLabel value="Name" for="name"/>
            <p:inputText id="name" value="#{sController.name}"/>

        </p:panelGrid>
    </p:panel>

    <p:commandButton value="Search" 
        actionListener="#{sController.findItem}"/>
</h:form>
</f:view>

2 个答案:

答案 0 :(得分:3)

这是一种解决方法。为了其他可能访问此页面同样问题的人的利益,您的网页无法导航,因为您在actionListener属性中使用的方法表达式不应该在那里使用(仅ActionListener类型那里有方法)。请在命令按钮的action属性中使用该方法。

正确实现ActionListener接口的方法的返回类型为void,因此不会隐式执行任何基于JSF的导航。然而,返回类型String的常规方法表达式是用于基于JSF的nav。

答案 1 :(得分:2)

  1. 删除你的faces-config文件(正如你所指出的那样)
  2. 用action:

    替换actionListener

    <p:commandButton value="Search" action="#{sController.findItem}"/>

  3. faces-redirect=true附加到您返回的字符串,以重定向到成功页面:

    public String findItem() {
        System.out.println("findItem called!");
    
        return "success?faces-redirect=true";
    }