PrimeFaces按钮导航操作

时间:2012-12-17 10:52:44

标签: ajax jsf primefaces

我在通过按钮导航页面时遇到问题。

我当前的xhtml代码段就是这样:

<p:layoutUnit position="west" size="200" header="Menü" resizable="true" closable="true" collapsible="true">
            <p:column>
                <p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create()}"/>


                <p:commandButton type="button" value="All Projects" icon="ui-icon-folder-open"/>

                <p:commandButton type="button" value="Edit" icon="ui-icon-pencil"/>

                <p:commandButton type="button" value="Delete" icon="ui-icon-closethick"/>

                <p:separator />
                <p:commandButton type="button" title="Yazdır" icon="ui-icon-print"></p:commandButton>
            </p:column>

        </p:layoutUnit>

在这里,当我点击New Project时,它不会导航到相关页面。

我的beanfile:

  @ManagedBean
@RequestScoped
public class CreateProject {
    /**
     * Creates a new instance of CreateProject
     */
    public String create() {
        return "newproject.xhtml";
    }
}

如何在单击按钮时导航用户?

编辑:这是我的faces-config

    <faces-config
    version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <managed-bean>
        <managed-bean-name>loginBean</managed-bean-name>
        <managed-bean-class>com.ibb.source.LoginBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>myProjects</managed-bean-name>
        <managed-bean-class>com.ibb.source.MyProjects</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>myCalendar</managed-bean-name>
        <managed-bean-class>com.ibb.source.MyCalendar</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <from-view-id>/panel.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>create</from-outcome>
            <to-view-id>newproject.xhtml</to-view-id>
        </navigation-case>
  </navigation-rule>
    <managed-bean>
        <managed-bean-name>createProject</managed-bean-name>
        <managed-bean-class>com.ibb.source.CreateProject</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
      </managed-bean>
        <managed-bean>
            <managed-bean-name>inPlaceEditor</managed-bean-name>
            <managed-bean-class>com.ibb.source.InPlaceEditor</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>
        <managed-bean>
            <managed-bean-name>projectsList</managed-bean-name>
            <managed-bean-class>com.ibb.source.ProjectsList</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>
        <navigation-rule>
           <from-view-id>panel.xhtml</from-view-id>
           <navigation-case>
               <from-outcome>newproject</from-outcome>
               <to-view-id>/newproject.xhtml</to-view-id>
           </navigation-case>
        </navigation-rule>
        <managed-bean>
            <managed-bean-name>allProjects</managed-bean-name>
            <managed-bean-class>com.ibb.source.AllProjects</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>
        <managed-bean>
            <managed-bean-name>allProjectsList</managed-bean-name>
            <managed-bean-class>com.ibb.source.AllProjectsList</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>
    </faces-config>

5 个答案:

答案 0 :(得分:7)

此代码:

type="button"

使commandbutton成为一个简单的javascript触发器。 如果要在服务器上执行任何操作,则必须删除type =“button”。 此外,在action标记上定义的方法必须返回与所需视图对应的字符串,并且您不需要使用xml来定义导航。 祝你有愉快的一天!

答案 1 :(得分:3)

默认情况下,PrimeFaces的commandButton组件使用ajax发送命令,因此导航将无法正常工作。将ajax属性设置为false

<p:commandButton ajax="false" value="New Project" icon="ui-icon-document" action="#{createProject.create()}" />

还要确保您的按钮位于表单元素中。在您的整个<h:form> ... </h:form>代码周围添加<p:layoutUnit>

答案 2 :(得分:2)

添加到faces-config.xml

<navigation-rule>
       <from-view-id>page1.xhtml</from-view-id>
       <navigation-case>
           <from-outcome>page2</from-outcome>
           <to-view-id>/newproject.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>



@ManagedBean
@RequestScoped
public class CreateProject {
    /**
     * Creates a new instance of CreateProject
     */
    public String create() {
        return "page2";
    }
}

 <p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create}"/>

答案 3 :(得分:2)

我遇到了同样的问题。我使用button代替commandButton解决了这个问题。请注意,outcome代替action

 <p:button ajax="false" value="New Project" icon="ui-icon-document" outcome="#{createProject.create}" />

答案 4 :(得分:1)

要在按commandButton时执行操作,您需要更改:

<p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create()}"/>

为:

<p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create}"/>

没有“()

方法返回的String可以是页面(如您的示例)或导航规则(faces-navigation)