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:
<dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
</dependency>
  • 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 s:textfield HTML input type text example</title>
        <s:head/>
    </head>
 
    <body>
        <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:actionerror/>
            </div>
        </s:if>               
        <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"/>            
        </s:form>
    </body> 
</html>  
  • ApplicationResources_en.properties
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!

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

@Results({
        @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;
    
    @Override
    @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:

30

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 *