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
kanishkpanwarAsked:
Who is Participating?
 
raj3060Connect With a Mentor Commented:
OK.

Now..

<HTML>
<HEAD>
<TITLE>Submit only if changed by Raj</TITLE>
</HEAD>
<BODY onload="loadValues();">
<form name='form1' 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>
<SCRIPT>
var changed=false;
var loadValue = new Array();
var elem = document.form1;
var len = elem.elements.length;  
function loadValues() {  
      for (i=0;i<len;i++) {
            loadValue[i] = elem.elements[i].value;
      }
}
function submitOrNot(){
        for (i=0;i<len;i++) {
            if(!(loadValue[i] == elem.elements[i].value)){            
                        return true;
            }
      }
      return false;
}
</SCRIPT>
</BODY>

</HTML>
0
 
raj3060Commented:
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.
0
 
kanishkpanwarAuthor Commented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
raj3060Commented:
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.
0
 
kanishkpanwarAuthor Commented:
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.
0
 
kanishkpanwarAuthor Commented:
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.
0
 
raj3060Commented:
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..
0
 
raj3060Commented:
firefox actually supports onchange event within FORM tag, but IE does not.
0
 
raj3060Commented:
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.
0
 
raj3060Commented:
Any Luck?
0
 
kanishkpanwarAuthor Commented:
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.
0
 
raj3060Commented:
?? Did it do the trick?
0
 
kanishkpanwarAuthor Commented:
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. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.