Fielderror Tag (Non-Form UI Tags) Example
In this section, we are going to describe the fielderror tags. The fielderror tag is a UI tag that renders field errors if they exists.
Add the following code snippet into the struts.xml
file.
struts.xml
<action name="fieldError"> <result>/pages/uiTags/loginFielderrorTag.jsp</result> </action> <action name="checkUser" class="net.roseindia.checkField"> <result name="input">/pages/uiTags/loginFielderrorTag.jsp</result> <result name="error">/pages/uiTags/fielderrorTag.jsp</result> <result>/pages/uiTags/validUser.jsp</result> </action> |
Develop an action class using addFieldError(String fieldName,
String errorMessage) method
. This method adds an error message
for a given field to the corresponding jsp page.
checkField.java
package net.roseindia;
|
Create a Login jsp page as shown:
loginFielderrorTag.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Fielderror Tag Example!</title>
<body>
<s:form action="checkUser" method="POST">
<s:textfield label="User Name" name="username" size="20" maxlength="10" />
<s:password label="Password" name="password" size="20" maxlength="10" />
<s:submit value="Submit" />
</s:form>
</body>
</html>
Create a jsp page that will display your field error
messages (when fails to logged-in) using the empty <s:fielderror
/>
tag as shown:
fielderrorTag.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
|
Create a jsp page that will display your messages
(when succeed to logged-in) using the empty <s:actionmessage />
tag as shown:
validUser.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
|
you will see the output of the loginFielderrorTag.jsp as shown below:
Enter the wrong user name and correct password in the login page
you will get the following output:
Enter the correct user name and wrong password in the login page
you will get the following output:
Enter incorrect values in both fields of the login page
you will get the following output:
Enter correct values in both fields of the login page
you will get the following output: