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

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

Both are generate the same HTML output (in default xhtml theme).

<td class="tdLabel">
  <label for="registerUser_username" class="label">Username:</label>
</td>
<td>
  <input type="text" name="username" value="" id="registerUser_username"/>
</td>
In Struts 2, the “name” will maps to the JavaBean property automatically. In this case, on form submit, the textbox value with “name=’username’” will call the corresponds Action’s setUsername(String xx) to set the value.

Struts 2 <s:textfield> example

Quick guide to create a textbox input field in Struts 2.

1. Properties file

Two properties files to store the message.

global.properties

#Global messages
username = Username
submit = Submit

RegisterAction.properties

#error message
username.required = Username is required

2. Action

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

RegisterAction.java

package com.mkyong.user.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class RegisterAction extends ActionSupport{
 
	private String username;
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	//business logic
	public String execute() {
 
		return "SUCCESS";
 
	}
 
	//simple validation
	public void validate(){
		if("".equals(getUsername())){
			addFieldError("username", getText("username.required"));
		}
	}
}

3. View page

Result page to use Struts 2 “s:textfield” to create a HTML textbox input field.

register.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 - textbox example</h1>
 
<s:form action="registerUser" namespace="/user">
 
	<s:textfield key="username" />
	<s:submit key="submit" name="submit" />
 
</s:form>
 
</body>
</html>

welcome.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 
<body>
<h1>Struts 2 - textbox example</h1>
 
<h4><s:property value="username"/></h4>
or
<h4><s:property value="%{username}"/></h4> 
 
</body>
</html>

4. struts.xml

Link all together ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
   <constant name="struts.custom.i18n.resources" value="global" />
   <constant name="struts.devMode" value="true" />
 
   <package name="user" namespace="/user" extends="struts-default">
	<action name="register">
		<result>pages/register.jsp</result>
	</action>
	<action name="registerUser" 
                class="com.mkyong.user.action.RegisterAction">
		<result name="SUCCESS">pages/welcome.jsp</result>
		<result name="input">pages/register.jsp</result>
	</action>
   </package>
 
</struts>

5. Demo

http://localhost:8080/Struts2Example/user/register.action

Struts 2 textbox example
Struts 2 textbox example

Reference

  1. Struts 2 textfield documentation
Note : You can find more similar articles at - Struts 2.x Tutorials