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" pageEncoding="UTF-8"%><! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="s" uri="/struts-tags"%>< html>< head>< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">< title>FieldError Tag Example</title></ head>< body>< s:actionmessage />< s:form action="ResultFieldErrorTag.action"> <s:textfield name="userName" key="Username"></s:textfield> <s:password name="password" key="Password:"></s:password> <s:submit></s:submit></ s:form></ body></ html> |
The Struts mapping file Struts.xml is as follows-
|
<? 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.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> <action name="ResultFieldErrorTag" class="roseindia.FieldErrorTag"> <result name="error">/FieldErrorTag.jsp</result> <result name="success">/result.jsp</result> </action> </package></ struts> |
The action class FieldErrorTag.java 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-
Output:-




