[Gukulkan]

Learn, develop, play.

Managed Beans in JSF and XHTML page

So, now we should connect bean with page.

Take this code:

@Named
@SessionScoped
public class HelloBean implements Serializable {

	//business logic and whatever methods...

}

Change it:

@Named("mybean")
@SessionScoped
public class HelloBean implements Serializable {
	public String myMethod(){
            return "Hello!";
        }

}

You can see that we change annotation Named and add one method, which return string “Hello!”  .

Now, show it on page.

<?xml version=”1.0″ encoding=”UTF-8″?>
<ui:composition template=”/WEB-INF/templates/default.xhtml”
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:a4j=”http://richfaces.org/a4j”
xmlns:rich=”http://richfaces.org/rich”
xmlns:my=”http://java.sun.com/jsf/composite/components”>
<ui:define name=”content”>

<h1>#{mybean.myMethod()} Welcome!</h1>
</ui:composition>

You can see string “Hello! Welcome!” on our page.

To be continued!