Actionerror and Actionmessage Tags (Non-Form UI Tags) Example
In this section, we are going to describe the actionerror and actionmessage tags. The actionerror tag is a UI tag that renders action errors (in the jsp pages.) if they exists while the actionmessage tag renders action messages if they exists.
Add the following code snippet into the struts.xml
file.
struts.xml
<action name="actionerrorTag"> <result>/pages/uiTags/Login.jsp</result> </action> <action name="login" class="net.roseindia.checkValidUser"> <result name="input">/pages/uiTags/Login.jsp</result> <result name="error">/pages/uiTags/error.jsp</result> <result>/pages/uiTags/validUser.jsp</result> </action> |
Create an action class that uses methods
addActionMessage(String)
and addActionError(String)
within the execute() method. The addActionMessage(String)
will
print the passed string on the success jsp page while the
addActionError
(String)
will
print the passed string on the error jsp page.
checkValidUser.java
package net.roseindia;
|
Create a Login jsp page as shown:
Login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Actionerror Tag Example!</title> <body> <s:form action="login " method="POST"> |
Create a jsp page that will display your error
message (when fails to logged-in) using the empty <s:actionerror />
tag as shown:
error.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
|
Create a jsp page that will display a message (when
successfully 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 Login.jsp as shown below:
Enter the wrong user name but correct password in the login page.
you will get the following output:
Enter the correct user name but incorrect password in 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: