How to know when FORM (bean) values changed by user or not

Many times, developer want to know whether user has changed the HTML FORM value or values so that at server side action they can take the necessary action if some values has been changed or not changed at all. For example, once someone has requirement regarding whether user changed the IP address fields in the UPDATE HTML FORM, because if that value has been changed by the user, then he need to make several heavy network calls, which he wants to avoid if user doesn't change that single value. Also, recently someone wants to know if the FORM has changed by single value or submitted as it without any change.

For this above scenario, there are two (best) possible solutions:

1. If you want to observe the value change for particular bean property, then use dummy variable in bean and if you do not wish to create one, then pass the previous bean value in query string.

How to make it:
public class MyForm
{ 
    private String name;
    private Integer number;
    // I like to know if this 'number' value was changed or not 
    // Crete a dummy variable for this "number"
    private Integer number_;
    /////////////////////////////
    public String getName() {
        return name;
    }
     public void setName(String string) {
        name = string;
    }
    public Integer getNumber() {
        return number;
    }
    public void setNumber(Integer i) {
        number = i;
    }  
    // Getter and setter for number_
    public Integer getNumber_() {
        return number;
    }
    public void setNumber_(Integer i) {
        number = i;
    }   
}

OR

If you don't wanna create extra dummy variable, pass value as query string.
<!-- pass the number value as query string to the action -->
<form action="/MyAction.do?number=${requestScope.number}>
        Name:   <input type="text" name="name"/>
        Number: <input type="text" name="number"/>
                <input type="submit" value="Submit"/>
</form>

The action path will be:
/MyAction.do?number=123

2. If you're not concerned about any particular value change but like to know whether form values are different than the previously shown values, then use Object#hashCode();

How to make it:
public class MyForm
{ 
    private String name;
    private Integer number;
    /////////////////////////////
    public String getName() {
        return name;
    }
     public void setName(String string) {
        name = string;
    }
    public Integer getNumber() {
        return number;
    }
    public void setNumber(Integer i) {
        number = i;
    }
    @Override
    /**
    * This is my logic for getting unique hashcode or you can implement you own
    */
    public int hashCode() {
       String hashCode =  name.hashCode() + "" + number.hashCode();
       return hashCode.hashCode();
    }
}

You have to pass the populated beans hashcode to the action class and compare it with the current bean instance; If they are same then it mean the bean values are unchanged…

Comments

BalusC said…
You're completely wrong.

A hashcode is not necessarily all-time unique. Think about it once again, a hashcode is an int, which means that it can hold "only" 2^32 values. Are there that much unique Strings? No, kid.
SagaR said…
Don't say it completely wrong, that's a one idea, a solution (which as per your thought, is not 100% correct, OK, I agree, Sir). It can help some beans where the fields are of different data type.

Popular Posts