JSF 2 textarea example

In JSF, you can use the <h:inputTextarea /> tag to render a HTML textarea field. For example,

JSF tag…


<h:inputTextarea cols="30" rows="10" />

Render this HTML code…


<textarea name="random value" cols="30" rows="10"></textarea>

JSF textarea example

A full JSF 2 example to render a textarea field via <h:inputTextarea /> tag.

1. Managed Bean

A managed bean, declared as name “user”.


package com.mkyong.form;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@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. View Page

Two pages for the demonstration.

demo.xhtml – render a textarea field via “h:inputTextarea”, button via “h:commandButton”, if the button is clicked, textarea value will be submitted to the “userBean.address’ property via setAddress() method, and forward to “user.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:body>
    	<h1>JSF 2 textarea example</h1>
 
	  <h:form>
		<table>
    		<tr>
    		  <td valign="top">Address :</td>
    		  <td><h:inputTextarea value="#{user.address}" cols="30" rows="10" /></td>
    		</tr> 
    		</table>
    		<h:commandButton value="Submit" action="user" />
    	  </h:form>
 
    </h:body>
</html>

user.xhtml – display the submitted textarea value via “h:outputText”


<?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:body>
    	<h1>JSF 2 textarea example</h1>
 
	 Address : <h:outputText value="#{user.address}" />
    </h:body>
</html>

3. Demo

URL : http://localhost:8080/JavaServerFaces/

Display “demo.xhtml” page

jsf2-textarea-example-1

If the button is clicked, display “user.xhtml” page, and also the submitted textarea value.

jsf2-textarea-example-2

Download Source Code

Download It – JSF-2-TextArea-Example.zip (9KB)

Reference

  1. JSF <h:inputTextarea /> JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
sandeep kiranfacilustestFahimSandip Pati Recent comment authors
newest oldest most voted
sandeep kiran
Guest
sandeep kiran

i have been developing a web based app in which i’m using spring+hibernate+jsf technologies. when i run >.xhtml file its showing like this error “Uh-oh, something went wrong! Error Code: 404 ” in browser and showing beancreation Exception in console….plz help me to get rid of this error. name which jar files are required for this application(i’m using Apche Tomcat(7))

facilus
Guest
facilus

Thank’s MyKong,

How can i insert html link in textarea for example value=”” ?

when i try to do like this,i’ve error because of the signe ‘<' should not be in jsf tag

test
Guest
test

test

Fahim
Guest
Fahim

Dear MyKong, What if I have text like below? this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample text… this is sample… Read more »

Sandip Pati
Guest
Sandip Pati

Hi MKyoung,

I am unable to paste MS word tables and bullets in jsf textarea.
How to do this.

It is come like this:

 Test
 Test
 Test

 A  B  C
 12  2  3
 w  w  w

Regards,
Sandip

Abdou
Guest
Abdou

Hello , Mkyong ;

thanks a lot for all these tutorials ..i have a question please about the component <h:inputTextarea , is there the same thing but for showing information , for example something like <h:outputTextarea … ?

thnaks anyway and good luck.

Ivan
Guest
Ivan

Hello, Mkyong!
I want to ask you about the storing of the data one’s entered in the inputTextArea field. I’ve been told that since the ManagedBean is SessionScoped, when the session expires all the data that has not been stored in a Data Base or some is lost.So how do I sore the data from the input field?
Thank you!

jewel
Guest
jewel

How about the textarea can be binding in “Enter” keypress when submit the message?

trackback
JSF 2.0 Tutorials

[…] JSF 2 textarea example <h:inputTextarea> textarea example. […]