Struts 2 stextarea input type textarea sample code

In this example you will see how to use s:textarea which is similar to html input type textarea . Below is sample which we use in our demo and also field label will be retrieved from ResourceBundle using key attribute:

<s:textarea label="Comments" name="comments" cols="30" rows="8"/>
  • Final project structure:

Struts 2 stextarea input type textarea sample code

  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?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>
<head>
<title>Struts 2 stextarea input type textarea sample code</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="label.welcome" />
	</h2>

	<s:form action="struts2SampleCodeAction" namespace="/" method="post"
		name="strutsForm">

		<s:textarea key="label.description" name="description" cols="30" rows="8"/>		
		<s:submit/>
	</s:form>
</body>
</html>
  • success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?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>
<head>
<title>Struts 2 stextarea input type textarea sample code</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="Value entered:" />
	</h2>

	<p>Description: <s:property value="description" /></p>	

</body>
</html>
  • Struts2SampleCodeAction.java
package com.javahonk.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Results({
		@Result(name = "error", location = "/index.jsp"),
		@Result(name = "success", location = "/success.jsp"),
		@Result(name = "input", location = "/index.jsp")})
public class Struts2SampleCodeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String description;
		
	@Override
	@Action(value = "/struts2SampleCodeAction")	
	public String execute() throws Exception {
		
		return ActionSupport.SUCCESS;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
		
}

 

  • Web page output:

Struts 2 stextarea input type textarea sample code

  • After click Submit button:

Struts 2 stextarea input type textarea sample code

download  Download Project:  Struts2ExampleCode

Leave a Reply

Your email address will not be published. Required fields are marked *