?
Solved

Struts - Compare formbean data with submitted form values

Posted on 2007-07-26
13
Medium Priority
?
155 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:kanishkpanwar
  • 8
  • 5
13 Comments
 
LVL 11

Expert Comment

by:raj3060
ID: 19576292
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
 

Author Comment

by:kanishkpanwar
ID: 19576415
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19576642
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kanishkpanwar
ID: 19576667
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
 

Author Comment

by:kanishkpanwar
ID: 19576770
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19577079
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19577093
firefox actually supports onchange event within FORM tag, but IE does not.
0
 
LVL 11

Expert Comment

by:raj3060
ID: 19577272
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19581888
Any Luck?
0
 

Author Comment

by:kanishkpanwar
ID: 19592645
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
 
LVL 11

Accepted Solution

by:
raj3060 earned 2000 total points
ID: 19608674
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19627233
?? Did it do the trick?
0
 

Author Comment

by:kanishkpanwar
ID: 19627268
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question