Skip to main content

Post 3: JSF: textarea example

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


Comments

Popular posts from this blog

Index: JSF

Java Server Faces Basics Post 1: JSF : Introduction to JSF  Post 2: JSF:  configuration files  Post 3: JSF: Installation Post 4: JSF: Your first JSF project   Standard JSF Tags Post 1: JSF: textbox example Post 2: JSF: password example Post 3: JSF: textarea example JSF 2 GraphicImage Example Post 4: JSF: GraphicImage Example Post 101 : JSF : The valueChangeListener Attribute Example

Index: iReport

iReport for Java ------------------------------------------------------------ Titles and Headers ------------------------------------------------------------ Post 1: iReport : Downloading, installing, and running JasperReports and iReport Post 2: iReport : Creating your first "Hello World" report   Post 3: iReport : Understanding iReport Elements Post 4: iReport : Creating and sizing the title for your report Post 5: iReport: Inserting a company logo in the title of your report  Post 6: iReport : Installation of PostgreSQL Post 7: iReport : Creating a report from relational data Post 8: iReport : Using dynamic titles that can change during report processing. Post 9: iReport : Adding a simple header to your report Post 10: iReport : Setting margins for your report and aligning the report header relative to report margins  ------------------------------------------------------------ Body and Footer ---------------------------------------------------------...