PrimeFaces button navigation action
Solution 1
This code:
type="button"
makes commandbutton a simple javascript trigger. If you want to do anything on the server, you must remove type="button". Also, your method defined on action tag, must return a string corresponding to your desired view, and you don`t need yo use an xml for defining navigation. Have a nice day!
Solution 2
By default, commandButton
component of the PrimeFaces, uses ajax to send the commands so navigations won't work. Set the ajax
property to false
:
<p:commandButton ajax="false" value="New Project" icon="ui-icon-document" action="#{createProject.create()}" />
Also make sure that your buttons are inside a form element. Put a <h:form> ... </h:form>
around your whole <p:layoutUnit>
tag.
Solution 3
I had the same problem. I solved it by using button
instead of commandButton
. Note that outcome
is used instead of action
.
<p:button ajax="false" value="New Project" icon="ui-icon-document" outcome="#{createProject.create}" />
Solution 4
Add into 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}"/>
Solution 5
To perform an action when pressing a commandButton
, you need to change:
<p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create()}"/>
to:
<p:commandButton type="button" value="New Project" icon="ui-icon-document" action="#{createProject.create}"/>
without the "()
"
The String returned by the method can either be a page (like your example) or a navigation rule (faces-navigation)
Ali Yucel Akgul
Updated on July 31, 2022Comments
-
Ali Yucel Akgul almost 2 years
I have a problem with navigating the pages through buttons.
My current xhtml snippet is like that:
<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>
Here, when I click on New Project it doesn't navigate to related page.
My beanfile:
@ManagedBean @RequestScoped public class CreateProject { /** * Creates a new instance of CreateProject */ public String create() { return "newproject.xhtml"; } }
How can I navigate user when the button is clicked?
edit: here is my 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>