Skip to main content

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.
techmythoughts 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 from this blog

Installing i3lock-color on Ubuntu

i3lock is fancy lock screen for Linux. There is i3lock dep available on Ubuntu but its not what I was looking for; I was more interested in i3lock-color . Lets see how we can install the same on ubuntu. PS: I'm using Ubuntu 16.04 Get source code for i3lock-color $ git clone https://github.com/PandorasFox/i3lock-color.git  $ cd i3lock-color Install required packages to build i3lock  $ apt install libev-dev $ apt install libxcb-composite0 libxcb-composite0-dev libxcb-xinerama0 libxcb-randr0  $ apt install libxcb-xinerama0-dev  $ apt install libxcb-xkb-dev  $ apt install libxcb-image0-dev  $ apt install libxcb-util-dev $ apt install libxkbcommon-x11-dev $ apt install libjpeg-turbo8-dev  $ apt install libpam0g-dev Build  $ autoreconf -i && ./configure && make Execute $ ./lock.sh Assign Shortcut In order to assign shortcut, install compizconfig $ sudo apt-get install compizconfig-settings-manager compiz-plugins-extra and then go

INTO OUTFILE query, How to modify & output null values ?

I tried one query (originally asked at JR) on MySQL which export the table data into a file, with a comma delimited field. The query is straight forward and replace the null column value by "\N" character, but when I want to replace that "\N" value by some other character like "0" or simple empty field like " ", its gets little tricky. Here is the base query SELECT Id, name, age FROM student INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; And it outputs like, 1, "sagar", 23 2, "anant", 24 Now suppose some 'name' fields are null, then it outputs like 1, "sagar", 23 2, \N, 24 Now to obtain my desired results, which replace this null (\N) values by empty string like, "", I tried out two solutions --1. Using CASE statement SELECT Id, CASE WHEN name IS NULL THEN '' ELSE name END AS NewName, age FROM s

GlassFish V3 admin console taking too much time to load.

If you have installed Glassfish V3 and trying to load admin console, but after signing in, is it taking too much time to get to the main page ? Do you have server.log entry like this: admin console: initSessionAttributes() Cannot refresh Catalog : Connection timed out then its time to tweak some files. Here its how: 1. Update the %GLASSFISH_HOME/glassfish/domains/domain1/domain.xml -Dcom.sun.enterprise.tools.admingui.NO_NETWORK=true This will block up the News item, the registration item, etc 2. Remove update tool jar (Backup and remove this JAR) %GLASSFISH_HOME/glassfish/modules/console-updatecenter-plugin.jar Delete this dir: %GLASSFISH_HOME/glassfish/domains/domain1/osgi-cache %GLASSFISH_HOME/glassfish/domains/domain1/generated Now start the server (bin/asadmin start-domain) and you will see the admin console won't be hang up and take you directly to main page.