Redirecting the page after adding a new user

UI Components for JSF
Post Reply
primer
Posts: 44
Joined: 23 May 2011, 15:25

04 Sep 2011, 17:58

Hello everybody,

I have a form which let to add a new user...
I want to redirect the user, automatically, to the "user list" page once he clicked on the add button (of course, the new user will be add to the database).

I managed to add the new user but not to redirect to the "user list" page !

this is the add button implementation :

Code: Select all

<p:commandButton value="Add" type="Submit" action="#{userController.newuser(user)}" update="posCenter"/>
this is the implementation of the newuser(user) method :

Code: Select all

  public String redirectUserList(){
        return "list_user.xhtml";
        }

public void newuser(User var){
        getFacade().create(var);
        System.out.println("Partie 100");
        liste = new ListDataModel(getFacade().findAll());
        redirectUserList();
    }

Any suggestions? Thanks ;)

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

04 Sep 2011, 18:03

1)
update="posCenter" is useless because you want to redirect

2)
you should use ajax="false" on your commandbutton

3)
return redirectUserList(); and change your action method to return type String would be better ;)
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

primer
Posts: 44
Joined: 23 May 2011, 15:25

04 Sep 2011, 21:26

I made exactly like what you said to me:

Code: Select all

  public String newuser(User var){
        getFacade().create(var);
        System.out.println("Partie 100");
        liste = new ListDataModel(getFacade().findAll());
        return "list_user.xhtml";
    }
and

Code: Select all

 <p:commandButton value="Add" type="Submit" action="#{userController.newuser(user)}" ajax="false"/>
But when I click on add, the form becomes empty (like when if I clicked on the reset button) and the redirection didn't happen !.... Another thing: the new user is not added (of course the System.out.println("Partie 100"); is also not showen) :?


Here also my form:

Code: Select all

 <h:form id="formulaire" enctype="multipart/form-data"  >
          <h:panelGrid columns="3" cellpadding="5">

              <h:outputLabel  value="Last name:" />
              <p:inputText value="#{user.nom}" id="lname" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="First Name:" />
            <p:inputText value="#{user.prenom}" id="fname" required="true"  />
            <input type="hidden"></input>

            <h:outputLabel  value="Address:" />
            <p:inputText value="#{user.adresse}" id="adr" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="Phone number:" />
            <p:inputMask value="#{user.tel}" id="tel" required="false" mask="99999999"/>
            <input type="hidden"></input>

            <h:outputLabel  value="Role:" />
            <h:selectOneMenu value="#{user.role}" >
                <f:selectItem itemValue="admin" />
                <f:selectItem itemValue="developper" />
            </h:selectOneMenu>
            <input type="hidden"></input>

            <h:outputLabel  value="Login:" />
            <p:inputText value="#{user.login}" id="lgn" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="Password:" />
            <p:inputText value="#{user.pwd}" id="passwd" required="true"/>
            <input type="hidden"></input>

            </h:panelGrid>
                <p:commandButton value="Add" type="Submit" action="#{userController.newuser(user)}" />
                <p:commandButton value="Reset" type="Reset" />

              </h:form>

User avatar
undermensch
Posts: 140
Joined: 08 Jul 2010, 14:37
Location: Florida, United States

04 Sep 2011, 22:48

Why are you returning "list_user.xhtml" in lieu of "list_user" or "list_user?faces-redirect=true"? & why not have user as a property of usercontroller? Then you don't need to pass a parameter in in action property....
PrimeFaces-11.0.6 / Wildfly 24

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

04 Sep 2011, 22:57

Try to remove enctype="multipart/form-data"
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

primer
Posts: 44
Joined: 23 May 2011, 15:25

04 Sep 2011, 23:02

& why not have user as a property of usercontroller? Then you don't need to pass a parameter in in action property....
do have a user as a property in my UserController Class, but when the create method don't work when I call it into the add button action ... I must pass a parameter (this is the solution that worked for me) !

primer
Posts: 44
Joined: 23 May 2011, 15:25

04 Sep 2011, 23:04

zoigl wrote:Try to remove enctype="multipart/form-data"
this is exactly the implementation of newuser.xhtml:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:composition template="/templates/template.xhtml">
    <head>
        <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/themes/bluesky/skin.css" />
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/css/roller/ui-darkness/theme.css" />
            <style type="text/css">
                .ui-widget {
                    font-size: 75%;
                }
                .themeMenu {
                    overflow: auto;
                    height:300px;
                    width:200px;
                }
                .ui-layout-north {
                    z-index:20 !important;
                    overflow:visible;
                }
                .ui-layout-north .ui-layout-unit-content {
                    overflow:visible;
                }
            </style>
    </head>

        <ui:define name="body">
             <h:body>
                 <center> <h4>NEW USER</h4>
            <h:form id="formulaire" enctype="multipart/form-data"  >
          <h:panelGrid columns="3" cellpadding="5">

              <h:outputLabel  value="Last name:" />
              <p:inputText value="#{user.nom}" id="lname" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="First Name:" />
            <p:inputText value="#{user.prenom}" id="fname" required="true"  />
            <input type="hidden"></input>

            <h:outputLabel  value="Address:" />
            <p:inputText value="#{user.adresse}" id="adr" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="Phone number:" />
            <p:inputMask value="#{user.tel}" id="tel" required="false" mask="99999999"/>
            <input type="hidden"></input>

            <h:outputLabel  value="Role:" />
            <h:selectOneMenu value="#{user.role}" >
                <f:selectItem itemValue="admin" />
                <f:selectItem itemValue="developper" />
            </h:selectOneMenu>
            <input type="hidden"></input>

            <h:outputLabel  value="Login:" />
            <p:inputText value="#{user.login}" id="lgn" required="true" />
            <input type="hidden"></input>

            <h:outputLabel  value="Password:" />
            <p:inputText value="#{user.pwd}" id="passwd" required="true"/>
            <input type="hidden"></input>

            </h:panelGrid>
                <p:commandButton value="Add" type="Submit" action="#{userController.newuser(user)}" />
                <p:commandButton value="Reset" type="Reset" />

              </h:form>
            </center>
                 </h:body>
            </ui:define>
    </ui:composition>
</html>

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

04 Sep 2011, 23:09

and now? try to remove it
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

primer
Posts: 44
Joined: 23 May 2011, 15:25

04 Sep 2011, 23:30

zoigl wrote:and now? try to remove it
It Works !!!!!!!!!!! ;) Thank you :mrgreen:

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests