Board index JavaServer Faces General Primefaces messages are shown twice

Primefaces messages are shown twice

Components, Ajax Framework, Utilities and More.


Posts: 5
Hello.

I have a question about primefaces messages. I have a flow where user can change his password. Before it is saved to database validation takes place. If the validation fails error messages are shown. The problem is that these messages are shown twice in different places: in the <p: messages> block defined in xhtml and in the form's right upper corner.

My version of primefaces is 2.2.1
Here is my flow:
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

   <action-state id="start">
      <evaluate expression="userService.getUser(currentUser.name)"
         result="flowScope.user"></evaluate>
      <transition to="view"></transition>
   </action-state>
   <view-state id="view" view="profile-view.xhtml">
      <transition on="edit" to="edit" />
      <transition on="cancel" to="cancel"></transition>
   </view-state>

   <view-state id="edit" view="profile-edit.xhtml">

      <transition on="back" to="start"></transition>
      <transition on="cancel" to="cancel"></transition>
      <transition on="save" to="actionSave"></transition>
   </view-state>

   <action-state id="actionSave">
      <evaluate expression="userValidator.validate()"></evaluate>
      <transition to="edit"></transition>
   </action-state>

   <end-state id="cancel"></end-state>
</flow>


Here is profile-edit.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:t="http://myfaces.apache.org/tomahawk"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://primefaces.prime.com.tr/ui"
   xsi:schemaLocation="
        http://java.sun.com/jsf/facelets facelets-ui-2.0.xsd
        http://java.sun.com/jsf/core jsf-core-2.0.xsd
        http://java.sun.com/jsf/html html-basic-2.0.xsd"
   template="/WEB-INF/layouts/standard.xhtml">
   <ui:define name="content">

      <h:form>
         <p:fieldset legend="Edit profile">
            <p:messages id="userFormMsgs"/>
            <div align="center">
               <h:panelGrid columns="2">
                  <h:outputLabel for="user" value="Username " />
                  <p:inputText id="user" value="#{userValidator.username}" />
                  <h:outputLabel for="old-pass" value="Old password " />
                  <p:password id="old-pass" value="#{userValidator.oldPass}"
                     feedback="false" label="Password" />
                  <h:outputLabel for="new-pass" value="New password " />
                  <p:password id="new-pass" value="#{userValidator.newPass}"  feedback="false" />
                  <h:outputLabel for="new-pass-confirm"
                     value="Confirm new password " />
                  <p:password id="new-pass-confirm" value="#{userValidator.newPassConfirmed}" feedback="false" />

               </h:panelGrid>
               <p:commandButton action="save" value="Save"
                  update="userFormMsgs" />
               <p:commandButton action="back" value="Back" />
               <p:commandButton action="cancel" value="Cancel" />
            </div>
         </p:fieldset>
      </h:form>
   </ui:define>
</ui:composition>


What may be the reason of this JSF behavior?

Thank you in advance.

kukeltje Expert Member

Posts: 6487
Location: Netherlands
The presence of 2 p:messages? or a p:messages and a normal one?
Ronald van Kuijk
______________________________
PrimeFaces 4.0, PrimeFaces plus 0.0.2 | Jboss 7.2.0 | Mojarra 2.1.18
Fedora 19, Firefox 26.0
Read the forum posting rules
Beginners: http://www.coreservlets.com/JSF-Tutorial/jsf2/


Posts: 5
Hello,

As you can see in the .xhtml page, there is only one p:messages.
Here is the code of validation which add the messages to context:
public String validate(User user) {
      FacesContext ctx = FacesContext.getCurrentInstance();
      
      EmailValidator emailValidator = new EmailValidator();
      boolean isEmailValid = emailValidator.isValid(username, null);
      
      if (( username == null) || (username.length() < 1)) {
         ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username cannot be empty!", ""));
      }
      if (!isEmailValid) {
         ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Email is not valid!", ""));
      }
      if (!user.getPassword().equals(oldPass)) {
         ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wrong password entered!", ""));
      }

      if (!newPass.equals(newPassConfirmed)) {
         ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "New password confirmation failed!", ""));
      } else {
         if (newPass.length() < 6) {
            ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password should be at least 6 symbols!", ""));
         }
      }

      if (ctx.getMessageList().size() > 0) {
         return (null);
      } else {
         user.setPassword(newPass);
         user.setUsername(username);
         repository.save(user);
         ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Profile updated!", ""));
         return (null);
      }


Could you please specify what you mean under "normal one"?

Thank you in advance.

kukeltje Expert Member

Posts: 6487
Location: Netherlands
It's a composition in a template. In the template can be another one. And a normal one is h:messages
Ronald van Kuijk
______________________________
PrimeFaces 4.0, PrimeFaces plus 0.0.2 | Jboss 7.2.0 | Mojarra 2.1.18
Fedora 19, Firefox 26.0
Read the forum posting rules
Beginners: http://www.coreservlets.com/JSF-Tutorial/jsf2/


Return to General