PDA

View Full Version : Request parameters How to


praedos
Jun 5th, 2008, 06:52 AM
Hi,

I have a problem with the request. My params are all empty. I don't know how to pass parameters to one page to another in Spring.

I put my parameters in the ModelAndView of the submit procedure and I try to get they in the formBackingObject but the request is empty.

In the other hand, I use a succes view that redirect my page to the another. How can I get the parameters?

Thanks,

Praedos

Sprang
Jun 5th, 2008, 08:05 AM
As you mention the formBackingObject method, I assume you're working with forms and want set form properties from the request when the form first loads.

If so, in your form config make sure you have the following in your config

<property name="bindOnNewForm" value="true"/>

praedos
Jun 5th, 2008, 09:55 AM
Hi Sprang,

It's that you said. I want to get the value of a field, a simple string.

I try to do this but don't run.

I use the SimpleFormController.

Perhaps the problem is that I do forward to a web that redirect to another web in the onSubmit() method.


mav.addObject("url","UW0009HeadForm.htm");
mav.setViewName("UW0000_Cigna_Succes");


In this web I do this:

<c:redirect url="${url}"/>

That is correct?

some one
Jun 5th, 2008, 07:20 PM
setSuccessView("redirect:/ URL")
return new ModelAndView(getSuccessView());

praedos
Jun 6th, 2008, 03:35 AM
Thank you,

I use this that you said but the problem continues.

To request my parameters I do this:

In onSubmit:


mav.addObject("parameter", "myParameter");


In formBackingObject:


System.out.println(request.getParameter("parameter"));


It's correct?

some one
Jun 6th, 2008, 11:36 AM
I dont think putting objects in the mv is the same as adding it to the HttpServletRequest .

What you will have to do is add the parameters to the url itself.

http://mysite.com?myparameter=val1

praedos
Jun 9th, 2008, 03:32 AM
Greetings,

It's doesn't work.

The request is empty in the other controller.

Thank you

praedos
Jun 9th, 2008, 06:01 AM
I can use the expresion ${user} in my JSP to use and object added on my ModelAndView of the page before.

How can I use this value in my formBackingObject method?

praedos
Jun 10th, 2008, 04:16 AM
nobody knows?

praedos
Jun 17th, 2008, 09:24 AM
OK!

In my opinion. The parameters are lost were I pass from the controller to a page that redirect to another page.

The problem is: How can I happen directly from a page to other one without intermediate pages?

Thanks to all!

Praedos

praedos
Jun 17th, 2008, 09:29 AM
When I try to do


setSuccessView("redirect:/UW0008LetterForm");


I have the error:


org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'headerCommand' available as request attribute

Sprang
Jun 17th, 2008, 09:54 AM
Can you explain exactly what you're trying to acheive (the flow of the pages)

praedos
Jun 17th, 2008, 11:35 AM
Ok,

I have a page that have a form with a command and I what to pass the values of the command to another page.


<bean name="/UW0007Document.htm" class="com.uw.cigna.dm.controller.J002LetterController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="headerCommand"/>
<property name="commandClass" value="com.uw.cigna.dm.command.HeaderCommand"/>
<property name="headerService" ref="headerService"/>
<property name="letterService" ref="letterService"/>
<property name="bindOnNewForm" value="true"/>
</bean>

<bean name="/UW0008LetterForm.htm" class="com.uw.cigna.dm.controller.J009LetterFormControlle r">
<property name="sessionForm" value="true"/>
<property name="commandName" value="headerCommand"/>
<property name="commandClass" value="com.uw.cigna.dm.command.HeaderCommand"/>
<property name="headerService" ref="headerService"/>
<property name="bindOnNewForm" value="true"/>
</bean>


When I try to pass to the first page to the second page I have the problem.


org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'headerCommand' available as request attribute


For this reason, I need to use a intermediate page.


model.addObject("url","UW0008LetterForm.htm");
model.setViewName("UW0000_Cigna_Succes");


In this page I redirect to the second page:


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
</head>
<body>
<c:redirect url="${url}"/>
</body>
</html>

praedos
Jun 17th, 2008, 11:37 AM
And the result is a empty request.

page1 -> page2 ERROR!!

page1 -> intermediate page -> page2 Empty request

Sprang
Jun 17th, 2008, 12:23 PM
A typical form using SimpleFormController has the following flow


Form is displayed to user (formView)
User inputs form values
User submits form
Input values are bound to command object and validation takes place
Application processes form values and does whatever business logic it needs to
Success page is displayed to user (successView)

Processing of the form should be done in the onSubmit() method.

Why do you want to pass the values to another page?

If this form is actually made up of different pages (like a wizard) then you can use AbstractWizardFormController or Spring Webflow.

If it is not and you want to pass the values from the form to another page (for whatever reason) then you should be able to return a redirect view and add the name/value pairs in the model to be bound to your new page.

So in your onsubmit method in your first form controller have

ModelAndView mav = ModelAndView("redirect:/newPage.htm");

And then add your form name/value pairs to the model

mav.addObject("headerCommand.name", "Sprang");
mav.addObject("headerCommand.location", "London");

return mav;

As you have bindOnNewForm set to true for your forms, they should bind these parameters as long as the paths are correct.

praedos
Jun 23rd, 2008, 03:56 AM
Thank You,

I try to build a AbstratctWizardFormController. It don't show any problem and the page is load correctly but when I push in any link the page reload and does not do anything more.

Why?

praedos
Jun 25th, 2008, 09:21 AM
AbstractWizardFormController don't enter in my onSubmit method. Why?

There are another method?

Sprang
Jun 25th, 2008, 09:27 AM
In the final jsp that is displayed before the form is finished make sure you have

<input type="hidden" name="_finish" value="true" />

in your form.

praedos
Jun 25th, 2008, 01:58 PM
Must I have only one controller for all the jsp?

lumpynose
Jun 25th, 2008, 04:02 PM
Praedos, I don't remember if Sprang mentioned this, but you want the submit from the form page to go back to the same/original controller. There are 2 http requests; the original GET which displays the form, and then when the user clicks the submit button there's a POST, which should also go back to the same controller which handled the GET. You do not want the form's ACTION to specify a url that will send it to a different controller. This is because if there are any errors in the user's input the controller can redisplay the form page for them.

lumpynose
Jun 25th, 2008, 04:34 PM
Must I have only one controller for all the jsp?
Get it to work with a single page form. Make it a two page web application; page 1 is the form where you enter the information, page 2 simply displays the information you entered in the form.

In the above when I say "page" I mean url. In Spring MVC urls are mapped to controllers. So one way to do this is to have page 1, the form page, have its own controller, and page 2, the display page, will have its own controller. Since they are separate urls you'll need to do a redirect at the end of page 1 after the form is submitted and validated, redirecting to page 2.

I think your question has to do with how you transfer the data that was entered into the form when you make the transition from page 1 to page 2.

You could let spring transfer the data to the next page, but it could be a lot, and when you do the redirect spring will put the data in the url as GET parameters since that's the only way spring can transfer the data for you. That's what happens when you put the data in the ModelAndView and then specify a view like

"redirect:page2"

You could instead store the data in a session variable. Or you could store it in a database, since you'll probably want it there eventually anyways, and just pass some sort of user id or transaction id between the pages in a session variable, or as a GET parameter. Then page 2 uses the userId to retrieve the data from the database, puts it in the model, and returns a ModelAndView that displays it.

In the form submit method for page 1 you can put the userId in the ModelAndView and specify the view with a redirect: and spring will add the userId to the GET parameter(s).

This may be more work than you'd like to do but I think it's a useful exercise.

A shorter way to do it would be to have page 1's controller, the form page, return the view that's the one from my above example that was used by page 2 (page2.jsp for example).

The other thing that I think can help clear up a lot of confusion is to not subclass any of the various spring form controllers, how it was done prior to spring 2.5, and use the annotation stuff instead. Then your controller is just a plain old class that's annotated with @Controller and you annotate its methods saying which respond to the GET request, the POST request, and the ones that provide the "reference data".

praedos
Jun 30th, 2008, 03:38 AM
It's run!

Thank you a lot, lumpynose!

And thank to all people that try to help me.