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.ADS_TO_REPLACE_1

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

pageEncoding="UTF-8"%>

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

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>ADS_TO_REPLACE_3

<head>

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

<title>FieldError Tag Example</title>ADS_TO_REPLACE_4

</head>

<body>

<s:actionmessage />ADS_TO_REPLACE_5

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

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

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

<s:submit></s:submit>

</s:form>

</body>ADS_TO_REPLACE_7

</html>

The Struts mapping file Struts.xml is as follows-

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

<!DOCTYPE struts PUBLICADS_TO_REPLACE_8

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

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>ADS_TO_REPLACE_9

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

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

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

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

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

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

</action>

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

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

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

</action>

</package>ADS_TO_REPLACE_13

</struts>

The action class FieldErrorTag.java is as follows.

package roseindia;

import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_14

public class FieldErrorTag extends ActionSupport {

private String userName;

private String password;ADS_TO_REPLACE_15

public String execute() {

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

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

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

return SUCCESS;

}ADS_TO_REPLACE_17

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

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

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

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

return ERROR;

}ADS_TO_REPLACE_19

public String getUserName() {

return userName;

}ADS_TO_REPLACE_20

public void setUserName(String userName) {

this.userName = userName;

}ADS_TO_REPLACE_21

public String getPassword() {

return password;

}ADS_TO_REPLACE_22

public void setPassword(String password) {

this.password = password;

}ADS_TO_REPLACE_23

public String display() {

return NONE;

}ADS_TO_REPLACE_24

}

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

Output:-

ADS_TO_REPLACE_25

ADS_TO_REPLACE_26

Download Select Source Code

Related Tags for Struts2.2.1 fielderror Tag Example:

Advertisements

Ads

 
Advertisement null

Ads