PDA

View Full Version : Best practice with Ajax and a form within


claude
Jun 2nd, 2006, 10:46 PM
I have built an Ajax page using JSP and Spring MVC (and I am not using DWR).

MainPage (view/index)
ajaxPart1 (view/sub/index)
ajaxPart2 (view/sub/edit)

view/index and view/sub/index are handled by the same subclass of MultiActionController

view/sub/edit is handled by a subclass of SimpleFormController.


Displaying the form in the structured described above works fine.
I have figured out a way (probably not the best way though) to redirect the succesful onSubmit back to the mainPage in a such way that it can display ajaxPart1 and ajaxPart2 based on the submitted form (i.e. it reloads the modified element).

Now I am struggling with error handling.

When there are errors, I want to reload

MainPage (view/index)
ajaxPart1 (view/sub/index)
ajaxPart2 (view/sub/edit)


with the form in ajaxPart2 displaying the errors. This means that I must somehow use view/index as the "formView" of my SimpleFormController, but I cannot figure out I could make error handling data retrieved by ajaxPart2 (the form).

This is hard to explain. Anyone follows me here? Has anyone done something similar? Somehow, I have a feeling this is going to remain unanswered...

sushmu
Jun 3rd, 2006, 08:46 AM
>> This is hard to explain. Anyone follows me here? Has anyone done something similar? Somehow, I have a feeling this is going to remain unanswered...

Ha ha.. How about an answer just to make you feel good :)

Ok.. let me try...

you have MP, AP1 and AP2

MP + AP1 = a controller
AP2 = another controller

AP2 does some processing and is getting independently handled by the controller. you also say this is ajax and so it does not reload the page.

With the information you have provided, there are still a lot of queries i have. But instead let me propose my solution.

1. All three forms must talk to the same command object.
2. The command object must be a session object.
3. When AP2 sees error, it can display it without any issues.. but however since you wanted a reload.. you can trigger a browser reload of the entire page.
4. Since all 3 forms are looking at the same command, irrespective of the form view, we are going to see the same data with your error messages intact.

Let me know if it helped.

Muthu Ramadoss
http://groups.google.com/group/etoe

claude
Jun 3rd, 2006, 01:48 PM
Ha... I feel better thank you :)

But I need more time to better understand your solution. This project was an intro to Ajax, and Spring MVC forms for me so...

When you say that the command object should be a session object, I think it is already if this is done by setting the property setFormSession(true) in my form controller. But still, reloading the full MainPage shows AjaxPart2 without the errors, as if command object was not picked up.

I am now leaning toward just showing a view in AjaxPart2, and showing the form in a stand alone page. Simpler. Maybe once I understand the Spring MVC better, I will know how to solve this.

sushmu
Jun 3rd, 2006, 11:40 PM
Claude,

Nice to know you are feeling good :)

After making it a session command object, make sure its being picked up from session in the 'formBackingObject' method in the controllers.

you can use the WebUtils#getSessionAttribute..

I do something like this...

protected Object formBackingObject(HttpServletRequest request)
throws
Exception
{

LoginModel loginModel = (LoginModel) WebUtils
.getSessionAttribute(request, getCommandName());

// create a new model, for the initial form
if (null == loginModel)
{
loginModel = new LoginModel();
// knibbler session does not exist yet
//todo: explicitly create session here ??
// loginLogic.init(request, loginModel, getCommandName());
loginLogic.prepareModel(loginModel);
}
return loginModel;
}

<bean id="loginController"
class="com.paypod.cm.knibble.web.controller.LoginControll er">
<property name="commandName" value="loginModel"/>
<property name="sessionForm" value="true"/>
<property name="formView" value="knibblerSignIn"/>
<property name="confirmView" value="registerConfirmation"/>
<property name="successView" value="redirect:/personality.html"/>
<property name="adminHomeView" value="redirect:/admin.html"/>
<property name="loginLogic" ref="loginLogic"/>
<property name="mailLogic" ref="mailLogic"/>
<property name="validator" ref="loginValidator"/>
</bean>

claude
Jun 4th, 2006, 05:00 PM
After making it a session command object, make sure its being picked up from session in the 'formBackingObject' method in the controllers.


Ok, I had missed that part. Thanks.