Home Struts Struts2 Struts2uitags Fielderror Tag (Non-Form UI Tags) Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Fielderror Tag (Non-Form UI Tags) Example

Advertisement
In this section, we are going to describe the fielderror tags.

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;
import com.opensymphony.xwork2.ActionSupport;

public class checkField 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;
  }
  if(!(getUsername().equals("Roseindia")))
  addFieldError("username","Invalid username!");

  if(!(getPassword().equals("Roseindia")))
  addFieldError("password","Invalid password!");

  return ERROR;
  }
  //Set and get the user name
  public void setUsername(String username){
  this.username = username;
  }
  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:
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" %>

<html>
  <head>
  <title>Fielderror Tag Example!</title>
  <body>
  <h1>
  <s:fielderror />
  
  </h1>
  <a href="/struts2tags/roseindia/fieldError.action">Go Back</a>
  </body>
</html>

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" %>

<html>
  <head>
  <title>Fielderror  Tag Example!</title>
  <body>
  <h1>
  <s:actionmessage />
  </h1>
  </body>
</html>

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:

     

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 28, 2007

Ask Questions?    Discuss: Fielderror Tag (Non-Form UI Tags) Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com