4. Struts : Reset(-,-) Method

Reset(-,-) FormBean class is called for each request before calling setters of a FormBean
Class

 If we override reset(,-) in a FormBean class then previous values are replaced with
default values in a FormBean object and after that the default values are again replaced
with new values submitted by a Client along with the request.

If reset(-,-):
Old values -----> default values -----> new values.


If no reset(,-):
Old values -----> Directly replaced with new Values

What is the difference between a Constructor and a reset(-,-) of a FormBean clas.
A) A Constructor of a FormBean class is called for once per session because a FormBean object is created

once for a session but reset(-,-) is called for each request submitted by a  client. If FormBean is assigned


LoginForm

_________________________________


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Satya<satyanarayana@greenbuds.co.in & satyajohnny@live.com>
 */
public class LoginForm extends org.apache.struts.action.ActionForm {
    
    private String uname;
     private String pwd;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
      ActionErrors errors = new ActionErrors();
      
      if(uname.isEmpty())
      {
      ActionMessage m1 = new ActionMessage("user.req");
      
      errors.add("uname", m1);
      
      } if(pwd.isEmpty())
      {
      ActionMessage m2 = new ActionMessage("pwd.req");
      
      errors.add("pwd",m2);
      
      }
      
      return errors;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        uname = "satya";
        pwd = "satya";
    }
     
    
}

Post a Comment

Thank You

Previous Post Next Post