Link to home
Start Free TrialLog in
Avatar of kanishkpanwar
kanishkpanwar

asked on

Struts - Compare formbean data with submitted form values

Hi,

I am wondering if there is any way in Struts 1.2.9 to compare incoming data from a form to existing formbean in session. I want to avoid calling update method if there is no change in any of the values on my form.

I know that I can use .equals() to check if instances are same. But where do I implement this method?

Thanks,
Kanishk
Avatar of raj3060
raj3060
Flag of United States of America image

Not too sure if you can do that.
Workaround would be to use client side validation i.e JavaScript. Check the Form values with Bean values and if they are same then don't submit else do.

Let me know if need and example.
Avatar of kanishkpanwar
kanishkpanwar

ASKER

http://cpd.ogi.edu/seminars04/tyhurstseminar_files/v3_document.htm

check this out.
author talks about it.
All I need to know is what method and what class do I need to override to compare the instance.
OK. Then you would have JavaBean to hold the existing Form values and then you would have FormBean holding the Form values that 's coming from your request object, inside your Action class create a method to compare JavaBean and FormBean values and then if there is no change then return everything back to user else update and then return to user.

I would rather user client side validation instead of submitting form values and then checking for changes to avoid the server call and to save time.
That is not a scalable approach, so I would stay away from it. Imagine having 20 fields on a freakin' form. *faints*
I think I tried your approach (Action form comparison) before with out success. But I'll give it a shot again.
nopes.
does not work. by the time call is in Actionclass, session instance form instance of the bean are same. I compared the hashcodes.
I suspect something needs to be done in Actionservlet.
You are worried to compare values with JavaScript.

here is a quick fix to that. This code will submit the form only if any of the form value is changed else not.

-----------------------
<HTML>
<HEAD>
<TITLE>Submit only if changed by Raj</TITLE>
<SCRIPT>
var changed=false;

function submitOrNot(){
      if(changed) {
            alert('I am changed');
            return true;
      }
      else return false;
}

function change(a) {
     changed=true;
}

function checkchanges() {
      var y = document.all;
      for (i=0;i<y.length;i++) {
            y[i].onchange = change;
      }
}
</SCRIPT>
</HEAD>
<BODY onload="checkchanges();">
<form onsubmit='return submitOrNot();'>
<P><input type=text name=TEXT1 value = 'HELLO' /></P>
<P><input type=text name=TEXT2 /></P>
<P><input type=text name=TEXT3 /></P>

<input type=submit value=Submit /></form>
</BODY>
</HTML>
-----------------------

Let me know how that goes..
firefox actually supports onchange event within FORM tag, but IE does not.
About the JAVA sol'n:

You would actually have two separate bean. JavaBean will be in Session, before you forward to the JSP you would load JavaBean with FormBean values. Now when form is submitted then insode ActionClass compare FormBean and JavaBean values.

This should work. It's tested.
Any Luck?
I already explored the idea of 2 beans. I think it will be pretty costly to memory considering I want to implement this feature all over the system.

Also, the javascript thing will fail in the scenario given below..

Delete a character from the textbox that you are checking for "onchange". Then type that character back and you have your text as changed (logically), even though it has not.
ASKER CERTIFIED SOLUTION
Avatar of raj3060
raj3060
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
?? Did it do the trick?
I did not try, but I think it should work. Though, this is not the approach that I am looking for.

Anyways, thanks for helping and enjoy your points. :)