Hi, I am developing a web application based on struts2, hibernate and spring. The problem is that the details are not being saved in Database. Always the control reaches to prepare() method and it never goes to the register() method (as defined in struts.xml). Before i fill the details the registration form will have the screen with the validaions on the empty fields (which is not correct - i am not getting why these fields are appearing).
The code is as follows:
Whenever add button is pressed again the same input form with validations are appearing instead of saving in database and getting the registration successful screen.
every thing is happening because of below interceptors
when you implement preparable interface inorder to prepare method it is called before every method on that class executes. so instead of initializing and prepopulating your form inside prepare method you put it inside input method as you could see this method is exempted from validation by validation interceptor.
which is causing your first problem i.e first time when you are viewing that form it's being validated which we don't want.
next problem is happening because again calling the prepare method before it goes to the register method. now what prepare method does send time is that it reinitializes the form which we difinitely don't want because we want the form data. and after that when validation framework works on it it it sees that form fields are not having proper value that's why it redirects you to input page.
so no prepare method. if you really want a prepare method use an input method and along with that use one method called prepareInput this is a convention in struts2 . and in that case if prepare interceptor calls it before calling input method.
There could be other ways to handle such scenario but i know only this one.
thanks ayan. The main problem is that the control is not going from prepare to register method. Any idea about why this is happening
posted 6 years ago
That's what i told for every method call prepare method would be called. now for fist time when you are displaying the form prepare method works fine. but after you fill up the form and submit it. It will go through interceptor stack. so again prepare method is executed which creates a new form instance and you loose all data filled up in form. then before your register method is called validation interceptor is called and it finds validation error.That's why it never reaches up to register method. it finds validation error and redirect you back to input page. remember what happens once all interceptors are executed successfully without any error then only method mapped to action is called.