Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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/uiT
ags/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="
loginmethod="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:

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

I Doahh, I'm also interested by the possibility to remove the dot preceding any actionMessage.

I've tried to add the following snipet in my basic css :

.actionMessage ul{
list-style-type: none;
}

.actionMessage li{
list-style-type: none;
}

But this doesn't give anything...

I've also tried to set the cssClass
attribute with "actionMessage" in the <s:actionmessage/> tag.

Have you got any suggestion on how to do that (I'm rather newbie with css,...)

Thanks,

Sébastien

Posted by vano on Tuesday, 06.10.08 @ 20:55pm | #62964

Write a css style for the ul and li html tags and set list-style-type: none as the rule.

Posted by doahh on Wednesday, 04.23.08 @ 17:12pm | #57695

how can i remove that ugly dot from action error

Posted by vinod on Monday, 02.4.08 @ 11:53am | #47175

how can i have a localization message?
ES:
addActionMessage("i18n.key.validuserString");

I use often server side annotation validation (@), they are localized.

Thanks

Posted by Luke on Monday, 12.10.07 @ 18:24pm | #41737

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.