1

I have written two managedbean classes named Message and HelloWorld. They are as follow :

Message.java :

package com.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "message", eager = true)
@RequestScoped
@SessionScoped
public class Message {

    private String message = "Hello World!";

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

HelloWorld.java

package com.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "helloWorld")
@RequestScoped
@SessionScoped
public class HelloWorld {

   @ManagedProperty(value="#{message}")
   private Message messageBean;
   private String msg;


   public HelloWorld() {
      System.out.println("HelloWorld started!"); 
   }

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

   public String getMessage() {
      if(messageBean != null){
         msg = messageBean.getMessage();
      }       
      return msg;
   }

   public void setMessageBean(Message message) {
      this.messageBean = message;
   }

   public void showMsg(){
      // String str="I am a demo string!";
       System.out.println(msg +" I am from showMsg()");
   }  
}

And, my index.xhtml here:

<body>
    #{helloWorld.message}
    <h:form>
        <h:commandButton value="Show Msg" action="#{helloworld.showMsg}"/>
    </h:form>
</body>

#{helloWorld.message} prints the message perfectly. But the <h:commandButton> does not invoke the methodshowMsg()`. What's the problem?

Tiny
  • 27,221
  • 105
  • 339
  • 599
mahbub_siddique
  • 1,755
  • 18
  • 22
  • 1
    `#{helloWorld.showMsg}` - note the capital W – dognose Dec 12 '14 at 15:22
  • 3
    And you should either use `RequestScope` or `SessionScope` - both together do not make any sense. – dognose Dec 12 '14 at 15:22
  • FYI : forthermore, `eager = true` is only applicable to application scoped JSF managed beans. It is as good as it is removed from the `@ManagedBean` annotation. – Tiny Dec 13 '14 at 03:06

3 Answers3

4

You have used action="#{helloworld.showMsg}" with lowercase w for world. EL is case sensitive. Change to action="#{helloWorld.showMsg}".

Also, what you have been told in the comments, you can't use both @RequestScoped and @SessionScoped, pick one. And, action attribute should resolve to String (showMsg() returns void), it is used to perform navigation. If you just want something done, without navigation, use actionListener instead.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1

A ManagedBean annotation doesn't work, if someone accidentally uses a different import, for example javax.annotation.ManagedBean. It must be javax.faces.bean.ManagedBean.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
  • This is not an answer to this actual problem... It could however show the same behaviour if you make this error. In addition, your 'answer' is in several other Q/A, amongst others: https://stackoverflow.com/questions/15057564/why-are-there-different-bean-management-annotations/15058240 – Kukeltje Oct 09 '17 at 19:12
0

EL is case sensitive in jsf. You have used action="#{helloworld.showMsg} with lowercase w and you declare @ManagedBean(name="helloWorld") with uppercase W. Try it, action="#{helloWorld.showMsg}.

user3687895
  • 163
  • 4