Struts 2 TextField Tag Example

Struts 2 s:textfield is the equivalent of input text box in the HTML. This tutorial shows how to take the input from a JSP file and pass on to the next field using Struts 2 Action class. Lets see the example.

1. Action Class

A simple action class for taking the employee name from user.

package javabeat.net.struts2;

public class Struts2UITagsAction{
	private String employeeName;

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public String execute(){
		return "success";
	}
}

2. TextField Tag Example

Input.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
	<h1>JavaBeat - Struts 2 Text Field Demo</h1>
	<s:form action="Result">
		<s:textfield name="employeeName" label="Employee Name" />
	<s:submit />
	</s:form>

</body>
</html>

Result.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
	<h1>JavaBeat - Struts 2 Text Field Demo</h1>
	<h4>
		Hello	<s:property value="employeeName" />
	</h4>

</body>
</html>

3. Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="textfielddemo" extends="struts-default">
	<action name="Result" class="javabeat.net.struts2.Struts2UITagsAction" >
		<result name="success">Result.jsp</result>
	</action>
</package>
</struts>

4. TextField Demo

If you access the application using URL http://localhost:8080/Struts2App/Input.jsp, you would see the following output.

struts2 textfield tag example input

 

struts2 textfield tag example output

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*