-1

I have made an input form that takes input and stores it in database using JSP and Servlets. The form has a EDIT DETAILS button. I want that when user clicks that button the form should autofill with old values and allow user to change those values. And the new values should get updated in the database. Here is the screenshot of the form. Please help...

enter image description here

Naman
  • 2,205
  • 2
  • 19
  • 32
Rachna Shriwas
  • 69
  • 2
  • 10
  • 1
    May this help :) http://javaknowledge.info/jsp-servlet-jstl-and-mysql-simple-crud-application/ – Naman Jun 04 '15 at 09:10
  • 1
    help with what? What you want is rather trivial, where do you experience problems? – jwenting Jun 04 '15 at 09:13
  • Can you be more specific with what you have done so far and where you need help? Its simple crud operation where first on page load you will get the values from DB and the populate in JSP. Track any changes and if changed save the update data back to database. – Sheetal Mohan Sharma Jun 04 '15 at 10:20
  • When page is loaded upper form is empty and Materials Details are displayed from DB. When user clicks edit details I want that previous details from DB should be displayed in the upper form and user should be able to change those and then finally submit. – Rachna Shriwas Jun 04 '15 at 10:37
  • @SheetalMohanSharma How to track the changes ? That's my probem.. – Rachna Shriwas Jun 05 '15 at 07:50
  • @rachna-shriwas check my answer below...is that what you want? – Sheetal Mohan Sharma Jun 05 '15 at 08:55

1 Answers1

0

It depends where you want to do. You can either do it client side or server side. For client site refer to post below. What is the best way to track changes in a form via javascript?

for server side

If you are ORM then you can use SaveOrUpdate() call. Read what it does.

OR

Make 2 dataTransfer objects lets call them loadTimeObject and submitObject.

Assuming flow and domain model here.

  1. Edit details button click -> get the row id or primary key/unique row/or id.
  2. Load data to fields by make Ajax call or if data is already available in JavaScript object, using row/object id - refer this as loadTimeObject
  3. User makes/does not make changes and click on submit. Load the form fields to submitObject. In Java validate if the loadTimeObject and submitObject differ? Track what fields changes and persist them in DB.

Hint:You may use Apache Commons Beanutils compareObjects(Object loadTimeObject , Object submitObject) methods.

OR

use java reflection to compare objects.

public static <T> void Compare(T source, T target) throws IllegalArgumentException, IllegalAccessException {

            if(source == null) {
                throw new IllegalArgumentException("Null argument not excepted at this point");
            }

            Field[] fields = source.getClass().getFields();

            Object sourceObject;
            Object targetObject;

            for(Field field : fields){
                sourceObject = field.get(source);
                targetObject = field.get(target);

                //Compare the object

            }

        }
Community
  • 1
  • 1
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24