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;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class checkValidUser extends ActionSupport {
private String username = null;
private String password = null;
public String execute() throws Exception{
if ((getUsername().equals("Roseindia")) &&
(getPassword().equals("Roseindia"))){
addActionMessage("Valid User!");
return SUCCESS;
}
else{
addActionError("Invalid User!");
return ERROR;
}
}
//Set and get the user name
public void setUsername(String name){
username = name;
}
public String getUsername(){
return username;
}
//set and get the password
public void setPassword(String pass){
password = pass;
}
public String getPassword(){
return password;
}
}
|
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">
<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 error
message (when fails to logged-in) using the empty <s:actionerror />
tag as shown:
error.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Actionerror Tag Example!</title>
<body>
<h1>
<s:actionerror />
</h1>
<a href="/struts2tags/roseindia/actionerrorTag.action">Go Back</a>
</body>
</html>
|
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" %>
<html>
<head>
<title>Actionerror Tag Example!</title>
<body>
<h1>
<s:actionmessage />
</h1>
</body>
</html>
|
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:
|