2

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

我当前的 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>

在这里,当我单击新项目时,它不会导航到相关页面。

我的豆文件:

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

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

编辑:这是我的面孔配置

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

5 回答 5

7

这段代码:

type="button"

使 commandbutton 成为一个简单的 javascript 触发器。如果你想在服务器上做任何事情,你必须删除 type="button"。此外,您在操作标签上定义的方法必须返回与您所需视图相对应的字符串,并且您不需要使用 xml 来定义导航。祝你今天过得愉快!

于 2012-12-17T12:23:17.197 回答
3

默认情况下,commandButtonPrimeFaces 的组件使用 ajax 发送命令,因此导航不起作用。将ajax属性设置为false

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

还要确保您的按钮位于表单元素内。<h:form> ... </h:form>在整个<p:layoutUnit>标签周围放一个。

于 2012-12-17T11:37:10.673 回答
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}"/>
于 2012-12-17T10:57:28.293 回答
2

我有同样的问题。我通过使用button而不是解决了它commandButton。请注意,outcome使用 代替action

 <p:button ajax="false" value="New Project" icon="ui-icon-document" outcome="#{createProject.create}" />
于 2014-09-27T15:31:03.967 回答
1

要在按下 a 时执行操作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}"/>

没有“ ()

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

于 2012-12-17T11:10:33.573 回答