Reputation: 5440
I need help, in understanding the way JSF connects with Java Class files. In the project i am currently into, it connects JSF with Java class using class objects. but i dont know where the object initialized. In Struts we connect JSP with action through Struts.xml. But here i dont see any mapping, it just calls the class name using
#{classNameObject.methodName}
here classNameObject
is an Object not a Class Name.
My question is how this #{classNameObject.methodName}
connects exactly to CLASSNAME?
Here is the Code sample
<p:dataTable id="dataTable" value="#{employeeList.empVarList}"
var="employee" emptyMessage="No Employees found" rows="15"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,15,20" rowIndexVar="rowIndex"
dynamic="false" cache="false" lazy="false"
style="display:block;overflow-y:none;overflow-x:auto;">
Here the dataTable connects to class EmployeeList
It uses JSF, primefaces, Spring, Hibernate
There is no initialization as employeeList
except this
private List<Employee> employeeList = new ArrayList<Employee>();
I am just a week experienced in all the above except hibernate. Please help me with all the anything you got. Thanks in advance.
Upvotes: 0
Views: 128
Reputation: 31649
JSF accesses the managed beans using Unified Expression Language, so-called EL. That's what you mention in your question as #{classNameObject.methodName}
and is an expression that is evaluated before the page is rendered.
Appart from that, you define a set of beans which will be available for accessing, you can do it either using annotations (@ManagedBean
or @Named
) or defining them in your faces-context xml file, which the framework will read when application is launched. Using xml file is more old-fashioned than annotations, that are actually more popular. All related annotations of your project are scanned by the framework when application starts via reflection.
For bean names, you can choose them yourself or, if you don't specify them, JSF will assign their class name with its first letter lowercased. That way you can reference them from EL.
You can also define a scope for your bean, that is used to define its lifetime. You can have a bean for the whole application, for each user session, for a concrete view or for each request. Also different beans are able to access the information from broader scope beans or from beans which are in the same scope. Have a look at this introduction by BalusC.
For the last point you mention, a property must have some value if you are able to see the data in the client side. Normally properties are initialized and populated before view renders, you can do it either in a @PostConstruct
method or using a preRenderView
event (you can do it also in bean's constructor method, but is not usual because normally the DAO access methods can throw Exceptions
). Also each property you want to access from EL must have its getter method.
Remember to use getters to read properties you already loaded, in this case you need to omit the get/is
word:
@ManagedBean
@ViewScoped
public Class PersonBean{
Person person;
public void postConstruct(){
person = loadPerson();
}
public Person getPerson(){
return person;
}
public void changeName(String newName){
person.setName(newName);
}
}
Access it:
<h:outputText value="#{personBean.person.name}" />
And modify it once it is loaded:
<h:commandButton value="Person name changer"
action="#{personBean.changeName('Ben')}" />
Upvotes: 2