Magazine
 
JSF Application
 

Now we come to those topics that have been left unexplained above.

Creating properties file (resource bundle):

In above JSP files we have used “message.properties” file. A properties file is a collection of “param=value” pairs. We can use there param names in our JSP file and get its value from here as we did previously. The benefit of using properties file is that we can modify these values easily in one place and ther is no need to change JSP files. In this application we have created “messages.properties” file in
javajazzup folder in WEB-INF/classes folder. The code for this file is:

messages.properties:

inputname_header=Java Jazz Up
prompt=Enter Your Name:
greeting_text=Welcome In Java Jazz Up
button_text=Submit

Creating Managed Bean:

In the above JSP files we have used Managed Bean named “StoreNameBean”. For this we have created “PersonBean.java” file. This Managed Bean is nothing but a Java file that contains attributes and setter and getter methods to set and get those attributes. Here in this example, there is only one attribute named “personName” and so only one setter method setPersonName() and one getter method getPersonName() is created. This bean is used to set the value to the bean attribute from the page and get the value from bean to the page. Make sure the attribute in this class must be same as the field name in JSP. In this example this file is created in package javajazzup. So compile this file and place its class file i.e. PersonBean.class in javajazzup folder in WEBINF\ classes folder. The code for this class is:

PersonBean.java:
package javajazzup;
public class PersonBean {
String personName;
public String getPersonName() {

 

return personName;
}
public void setPersonName(String name) {
personName = name;
}
}

If you want to access the bean classes in your JSP files, you have to register the bean classes in faces-config.xml.

Registering managed bean:

We have already created faces-config.xml file with empty <faces-config> element. This configuration file is used to register managed beans, specifying navigation rules etc. We will add <managed-bean> element within <facesconfig> and </faces-config> tag to register Managed Bean.

<managed-bean>
<managed-bean-name>StoreNameBean</
managed-bean-name>
<managed-bean-class>
javajazzup.PersonBean</managed-beanclass>
<managed-bean-scope>request</managedbean-
scope>
</managed-bean>

Bean’name is given in <managed-beanname> tag. This name is used in JSP files to represent the bean. The class name that corresponds to this bean is given in <managedbean- class> tag. <managed-bean-scope> defines the scope for the bean. In this Application, name of the bean that will be used in JSP files is “StoreNameBean”.

Defining navigation rule:

Now we will understand how navigation from one page to the next page is performed as in our application inputname.jsp page navigates to result.jsp page when user presses submit button after filling text in input text field. To understand this we come back to the line of code used in “inputname.jsp”:

Oct 2007 | Java Jazz Up | 59
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF