Pages Navigation Menu

Coding is much easier than you think

Struts 2 <s:textfield> example

Struts 2 <s:textfield> example

In Struts 2 , you can use the <s:textfield> to create a HTML input textbox. For example, you can declare the €œs:textfield€ with a key attribute or label and name attribute.

<s:textfield key="userName" />
<s:textfield label="Username" name="userName" />


In Struts 2, the “€œname” will map to the JavaBean property automatically. In this case, on form submit, the textbox value with €œname=”userName”™€ will call the corresponding Action’€™s setUsername(String xx) to set the value

** UPDATE: Struts 2 Complete tutorial now available here.

Struts 2 <s:textfield> example


1. Properties file

Two properties files to store the message.

project.username = Username
project.submit = Submit


username.required = Username Cannot be blank


2. Action

A simple Action class with a validation to make sure the username is not empty, otherwise return an error message.

package com.simplecode.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 6677091252031583948L;

	// Need to Initiize a morden driven object
	private String userName;
	private String password;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;

	public String execute() {

		return SUCCESS;

	public void validate() {
		if (userName.equals("")) {
			addFieldError("userName", getText("username.required"));


3. View page

Result page to use Struts 2 €œs:textfield€ to create a HTML textbox input field.

<%@taglib uri="/struts-tags" prefix="s"%>
	<h3>Struts 2  &lt; S:textfield &gt; Textbox Examplee</h3>
	<s:form action="welcome">
		<s:textfield name="userName" key="project.username" />
		<s:submit key="project.submit" name="submit" />


<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Welcome Page</title>

	<h3>Struts 2 &lt; S:textfield &gt; Textbox Example</h3>
		Welcome <s:property value="userName" />


4. struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

  <constant name="struts.custom.i18n.resources" value="project" /> 
	<package name="default" extends="struts-default" namespace="/jsp">
	<action name="Login">
		<action name="welcome" class="com.simplecode.action.LoginAction">
			<result name="success">/jsp/success.jsp</result>
			<result name="input">/jsp/login.jsp</result>


5. Demo



struts textbox



Struts 2 textfield documentation

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: