Thodoris Bais

About Thodoris Bais

Thodoris Bais is currently working as a Junior Java Developer while on parallel, pursuing a diploma at Informatics & Telecommunications Engineering. He specializes in Java Core, Android and J2EE and looks forward to being a professional Software Engineer. He is also a quick learner and curious about new technologies.

Textarea Example with JSF 2.0

In this example of JSF Tag Library series, we are going to show an effective way to implement a textarea field. Suppose that we want to insert a textarea of 20 columns and 10 rows. In HTML, this means <textarea cols="20" rows="10"></textarea>. According to JSF, we can use the following tag, to implement it: <h:inputTextarea cols="20" rows="10" />.

So, let’ s get the work done!
 
 
 
 

1. Managed Bean

As usually, I’ll first provide the source code for the Managed Bean, but for this time, we ‘ll hack it a little bit, by changing its name property, to user. That is, our web pages, can refer to the Managed Bean by using the custom names that we had given to them, during development.

UserBean.java

package com.javacodegeeks.enterprise.jsf.textarea;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{

	private String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}	
}

2. Our Pages

As in the previous example, we need two separate pages; Let’s have a look at them:

index.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">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
    	<title>JSF Textarea Example</title>
    </h:head>
    <h:body> 
    	<h1>JSF 2.0 Textarea Example</h1>
    	<h:form>
    		<table>
    		<tr>
    			<td valign="top">Address:</td>
    			<td><h:inputTextarea> value="#{user.address}" cols="20" rows="10" /></td>
    		</tr>
    		</table>
    		<h:commandButton value="Submit" action="response" />
    	</h:form>
    </h:body>
</html>

response.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">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
    	<title>JSF Textarea Example</title>
    </h:head>
    <h:body> 
    	<h1>JSF 2.0 Textarea Example - Response Page</h1>
    	
    	Your address is : <h:outputText value="#{user.address}" />
    </h:body>
</html>

3. Demo

Let’s take a quick demo, by trying to access the following URL: http://localhost:8080/TextareaJSF

image

And after clicking the button, our response page:

image2

4. Some Closing Words

There is a chance to get an execution error; you will be informed that the class user cannot be found. In this case, we usually try to handle the Managed Bean’s name annotation by hand, by simply adding a managed bean dependency in faces-config.xml file. For this reason, I’m attaching this version of the project, just in order to keep every newbie to J2EE following along, without facing any difficulty.

This was an example of Textarea in JSF 2.0. You can also download the source code for this example: TextareaJSF

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books