2

Am a newbie to JSF. Am using JSF 2 and primefaces 4.0 in my application. As stated in the Title, the input value given in the xhtml page, does not set the value to the ManagedBean. I have tried all the possible combination.

growlMessage.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>

    </h:head>

   <h:body>
    <h:form> 
  <p:growl id="growl" showDetail="true" sticky="true" />  

    <p:panel id="panelID" header="Growl">  
        <h:panelGrid columns="2" cellpadding="5">  
            <h:outputLabel for="msg" value="Message:" />   
            <p:inputText id="msg" value="#{growlView.message}" required="true" />  
        </h:panelGrid>  
      <p:commandButton value="Save" actionListener="#{growlView.saveMessage}"/>  
    </p:panel> 

   </h:form>  
    </h:body>
</html>

` GrowlView.java:

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;


@ManagedBean
@ViewScoped
public class GrowlView implements Serializable{

    private String message;

    public GrowlView() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public void saveMessage(){
        System.out.println("@@@@@  hello");
        System.out.println("@@@@@"+ getMessage());
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Successful", "Your message: "+message));
        context.addMessage(null, new FacesMessage("Second message", "Additional Message details"));
     }
  }
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Mogana
  • 262
  • 1
  • 4
  • 14

4 Answers4

0

Do you have a good reason to use JSF 2.0 instead of 2.2? You should use CDI instead of JSF managed beans, which is more or less deprecated. So, use

@Named
@ViewScoped
public class GrowlView implements Serializable

Be sure the ViewScoped annotation is from javax.faces.view. And the beginning of the xhtml should use the new namespace:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui">
Jaqen H'ghar
  • 4,293
  • 2
  • 12
  • 24
  • 1
    Why should he do that? JSF 2.0 works fine. You don't always have to add such huge things like CDI to your project if it isn't crucial. – LarsBauer Jun 07 '14 at 15:32
  • Because I tested his code and only got it working when doing this. Oh, and because it is recommended to move towards CDI: https://weblogs.java.net/blog/mriem/archive/2013/10/30/jsf-tip-30-migrate-your-managedbean-named-annotations and http://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named. For at beginner it gives no meaning AT ALL to start out with JSF managed beans, unless you know you are going to maintain an existing system. But thank you very much for the downvote. – Jaqen H'ghar Jun 07 '14 at 15:34
  • In this easy example CDI won't be necessary. – LarsBauer Jun 07 '14 at 15:36
  • Thanks for everyone's effort. I found the solution and my application is running correctly – Mogana Jun 10 '14 at 06:40
0

Your commandButton should look like this (according to PrimeFaces showcase):

<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl"/>

Have you tried setting a larger scope to your managed bean, for example @SessionScoped ? (just for testing purposes). So you could exclude a possible scope problem.

LarsBauer
  • 1,509
  • 18
  • 22
0

try this following code: using the process and partialSubmit attributes:

<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl" process="@form" partialSubmit="true"/>
-1

hy,

change for

 <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>

    </h:head>

   <h:body>
    <h:form> 
  <p:growl id="growl" showDetail="true" sticky="true" autoUpdate="true"/>  

    <p:panel id="panelID" header="Growl">  
        <h:panelGrid columns="2" cellpadding="5">  
            <h:outputLabel for="msg" value="Message:" />   
            <p:inputText id="msg" value="#{pageView.message}" required="true" />  
        </h:panelGrid>  
      <p:commandButton value="Save" action="#{pageView.saveMessage}" update="growl"/>  
    </p:panel> 

   </h:form>  
    </h:body>
</html>

and replace

@ManagedBean

with

@ManagedBean(name="pageView")
sghaier ali
  • 433
  • 2
  • 14