tags:

views:

371

answers:

2

Hello, It seems i am not clear on my previous question about managed bean. So, i am posting it again in a more systematic manner.

I have one page :-

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <ui:insert name="head">

        </ui:insert>

        <br/><br/>
        <h:inputText value="#{MasterBean.laala}"/>
        <br/><br/>

        <ui:insert name="content">

        </ui:insert>
    </h:body>
</html>

This is it's bean :-

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


@ManagedBean(name="MasterBean")
@ViewScoped
public class MasterBean {

    private String laala;

    public String getLaala() {
        return laala;
    }

    public void setLaala(String laala) {
        this.laala = laala;
    }


    public MasterBean() {
    }

}

This is data.xhtml which uses master.xhtml:

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <ui:composition template="master.xhtml">
            <ui:define name="head">
                laala
            </ui:define>

            <h:outputText value="#{pageBean.content}"/>

            <ui:define name="content">
                <h:commandButton actionListener="#{pageBean.speakHello}"/>
            </ui:define>>


        </ui:composition>
    </h:body>
</html>

This is it's bean :-

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


@ManagedBean(name="pageBean")
@ViewScoped
public class pageBean {

    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public void speakHello(){
        //how do i get value of laaala here which is in MasterBean???
        content = ??? + " Hello friend";
    }

    public pageBean() {
    }

}

Please fill in the blank where ??? is marked in speakHello(). How do i get value of other bean in this bean? I think this time i am very clear. Please BalusC, Bozho and other java experts, let me know if, still i am not clear.

Thanks in advance :) This time i surely expect a answer :)

+1  A: 

As you can learn in a JSF2 tutorial one can use the dependency injection feature. In your case it would be about injecting MasterBean instance to pageBean. I believe you could have something like this in your pageBean:

@ManagedBean(name="pageBean")
@ViewScoped
public class pageBean {

    private String content;

    @ManagedProperty(value="#{MasterBean}")
    private MasterBean masterBean;

    ...

a then use it in your methods:

 public void speakHello(){
    //how do i get value of laaala here which is in MasterBean???
    content = masterBean.getLaala() + " Hello friend";
 }

I haven't tried that myself. I hope it will work.

BTW, it's highly recommended not to start class name with a small letter. So pageBean class should be called PageBean instead.

Grzegorz Oledzki
Great! GrzegorzOledzki :). Vote and tick mark for correct answer. First, i got exception but fortunately it threw meaningful exception that getter and setters were not there. Once i created them, it's working smooth and fine. Wow thanks again :)
Ankit Rathod
+2  A: 

There is a perfect answer already!

Communication in JSF

kazanaki
This is however a *bit* outdated (4 years old!). I've to catch up the new JSF2 annotations yet :)
BalusC