Struts 2 stextfield HTML input type text example

Struts 2 stextfield HTML input type text example

In struts 2 application you could define HTML input type text by using below s:textfield as shown below:

<s:textfield name=”firstName” key=”label.firstName”/>
or with attributes:
<s:textfield name=”firstName” size=”30″ maxlength=”50″ label=”First Name”/>

As you see above there is two way to define label:

  • You could directly use label=”First Name” to give field name
  • OR use key=”label.firstName” which will be read from resource bundle

Please see example below:

  • Project structure:

Struts 2 stextfield HTML input type text example

  • pom.xml:

  • 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" "">
        <title>Struts 2 s:textfield HTML input type text example</title>
        <h2 style="color: green"><s:text name="Struts 2 s:textfield HTML input type text example" /></h2>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
        <s:form action="textFieldDemoAction" namespace="/" method="post" name="strutsForm">
            <s:textfield name="firstName" size="30" maxlength="50" key="label.firstName"/>
            <s:textfield name="lastName" size="30" maxlength="50" key="label.lastName"/>
            <s:submit type="button" key="submit" label="Submit" align="right"/>            
label.welcome = Struts 2 <s:textfield> input type text example
label.firstName =  First Name
label.lastName =  Last Name

error.firstName.required = First Name is required!
error.lastName.required = Last Name is required!

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;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

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

    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    @Action(value = "/textFieldDemoAction") 
    public String execute() throws Exception {
        return ActionSupport.INPUT;

    public String getFirstName() {
        return firstName;

    @RequiredStringValidator(key = "error.firstName.required")
    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    @RequiredStringValidator(key = "error.lastName.required")
    public void setLastName(String lastName) {
        this.lastName = lastName;

  • Run application on tomcat server and you will see below output:

Struts 2 stextfield HTML input type text example

  • With validation:

Struts 2 stextfield HTML input type text example

  • With data:


For more information please read this tutorial

download Download Project:   Struts2TextFieldDemo

Leave a Reply

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

I am not Robot *