What is an ActionForm?

An ActionForm is also called JavaBean.When client will enter the data from the form than this object is automatically populated on the server side. 


ActionForm class maintains the session state for web application.The ActionForm class extends from org.apache.struts.action.ActionForm. ActionForm class have getter and setter method. Getter method  is used to get the value from the form and setter method is used to set the value.
Struts Framework provides the functionality to validate the form data. When client enter the input from form if We want to add some validation than We can add the validate() method in the ActionForm class.Error messages are added to the ActionMapping object.
We can be use to validate the data on the users browser as well as on the server side. 

SourceCode:



package com.wisedeveloper.co.in;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends org.apache.struts.action.ActionForm{
               private static final long serialVersionUID = 11112;
               private String username,password;
               public String getPassword() {
                               return password;
               }
               public void setPassword(String password) {
                               this.password = password;
               }
               public String getUsername() {
                               return username;
               }
               public void setUsername(String username) {
                               this.username = username;
               }
               public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
                               ActionErrors errors = new ActionErrors();
                               if (username == null || username.length() < 1) {
                               errors.add("username", new ActionMessage("error.username.required"));
                               }
                               else if (password == null || password.length() < 1) {
                               errors.add("password", new ActionMessage("error.password.required"));
                               }
                              
                               return errors;
               }
               }

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More