вторник, 17 апреля 2012 г.

Understanding JSF - Part 2 - Accessing Beans

If you are just starting with JSF I would recommend you to read the previous tutorials. In this tutorial I will explain how to access your Managed Beans from JSF view pages. We will use a JSF application which we have developed in the previous tutorial.

Managed Beans - Deeper look

Java bean - is a Java class which gives the access to its variables and methods to JSF pages. You are accessing your beans using a declaration of get/set method of your variables. For example, if you have a variable name, the get method will be getName() (first letter of the variable in the method which corresponds to this variable have to be uppercase).

Here is the example of the simple bean:

package org.netlink.examples;
import javax.inject.*;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
@Named
@SessionScoped

public class MyBean implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1. @Named - this annotation indicates that we are using CDI (Context & Dependency Injection) bean. It is better thus we do not have to specify our bean in faces-config.xml. Using @Named annotation we will be able to access our bean from JSF view page using EL #{myBean.name}. We could also write @Named(name="myBestBean") and use #{myBestBean.name).

2. @SessionScoped - it is the scope of the bean. SessionScoped means that our bean will live through out the session. We will have a deeper look on bean scopes later in other tutorials.

3. Serializable - CDI beans must also implement Serializable interface.

So, now we can access our bean from JSF page. Here is an example:

1. home.xhtml


<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">
<h:head>
</h:head>
<h:body>
<h:form>
Enter your name: <br />
<h:inputText value="#{myBean.name}" /><br />
<h:commandButton action="welcome" value="Welcome!"></h:commandButton>
</h:form>
</h:body>
</ui:composition>

Note: every input should be nested in h:form.

2. welcome.xhtml


<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">


<h:outputText value="Welcome #{myBean.name} !" />
</ui:composition>

3. The last thing you have to add is an empty beans.xml file:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:s="urn:java:ee"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd"></beans> 

Now deploy your application, start jboss and go to localhost:8080/helloworld





















If you have any questions please write them in comments.

Best regards,
Netlink community member

Комментариев нет:

Отправить комментарий