Download this Struts text box example –

In this Struts example, you will learn how to create a HTML text box input field with Struts <html:text> tag, validate the text box with ActionForm, display the error message with ActionErrors, and also message resource in JSP page.

1. Folder Structure

This is the final project structure created by Maven. Please create the corresponding folders.


2. Action class

Create an Action class, do nothing but forward the request.

package com.mkyong.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserAction extends Action{
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
		return mapping.findForward("success");

3. Properties file

Create a properties file, and declare the error and label messages.

#common module error message = Name is required.

#common module label message = UserName
label.common.button.submit = Submit
label.common.button.reset = Reset

4. ActionForm

Create a ActionForm, accept a username and validate it in validate() method. If the username is empty, get a error message from the above properties file, and add it as key “” in ActionErrors, later will use this name “” to display the error message in JSP.

package com.mkyong.common.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserForm extends ActionForm{
	String username;

	public String getUsername() {
		return username;

	public void setUsername(String username) {
		this.username = username;

	public ActionErrors validate(ActionMapping mapping,
	HttpServletRequest request) {
	   ActionErrors errors = new ActionErrors();
	    if( getUsername() == null || ("".equals(getUsername()))) {
                         new ActionMessage(""));
	    return errors;
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		username = "";

5. JSP Page

Use the Struts’s HTML tag <html:text> to create a HTML text box input field. The label message is declared in above properties file, you can get it via <bean:message key=”label_name” /> directly.

The error message can display in two ways :

  1. Display all error messages
  2. Display specified error message by specify the error message key which declared in ActionErrors.


<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="bean"%>

Struts html:text example


Display the text box input.

<%@taglib uri="" prefix="bean"%>


6. struts-config.xml

Create a Struts configuration file and link all together.

In action tag attributes :

  • path = web path you access
  • type = your action class
  • name = your action form
  • validate = true will cause ActionForm to validate() method for the form validation
  • input = if the validation false, where to forward?

You have to include your properties file in struts-config.xml file to make Struts aware of your custom resource message.

	parameter="" />

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 



			<forward name="success" path="/pages/welcome.jsp"/>

		parameter="" />


7. web.xml

Final step, create a web.xml for the Strut framework integration.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Maven Struts Examples</display-name>


Access it



If user didn’t fill in the text box and press the submit button, it will display a error message.


If user fill in the text box and press the submit button.


It will pass the text box input value to the welcome page and display it.