Struts2.2.1 fielderror Tag Example
Posted on: January 17, 2011 at 12:00 AM
The fielderror tag is a UI tag that render field errors if they exists.

Struts2.2.1 fielderror Tag Example

The fielderror tag is a UI tag that render field errors if they exists.

The following Example will shows how to implement the fielderror tag in the Struts2.2.1 --

First we create a JSP file named FieldErrorTag.jsp as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


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



<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>FieldError Tag Example</title>



<s:actionmessage />

<s:form action="ResultFieldErrorTag.action">

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

<s:password name="password" key="Password:"></s:password>





The Struts mapping file Struts.xml is as follows-

<?xml version="1.0" encoding="UTF-8"?>


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"



<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<constant name="struts.custom.i18n.resources" value="ApplicationResources" />

<package name="roseindia" extends="struts-default" namespace="/">

<action name="FieldErrorTag" class="roseindia.FieldErrorTag" method="display">

<result name="none">/FieldErrorTag.jsp</result>


<action name="ResultFieldErrorTag" class="roseindia.FieldErrorTag">

<result name="error">/FieldErrorTag.jsp</result>

<result name="success">/result.jsp</result>




The action class is as follows.

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class FieldErrorTag extends ActionSupport {

private String userName;

private String password;

public String execute() {

if (getUserName().equals("Roseindia")

&& getPassword().equals("Roseindia")) {

addActionMessage("You are a valid user.");

return SUCCESS;


if (!(getUserName().equals("Roseindia")))

addFieldError("userName", "Invalid username!");

if (!(getPassword().equals("Roseindia")))

addFieldError("password", "Invalid password!");

return ERROR;


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 display() {

return NONE;



This Program produces output on the basis of the fielderror  Tag  evaluation, This  give the output as-


Download Select Source Code

Related Tags for Struts2.2.1 fielderror Tag Example:




Advertisement null