PDA

View Full Version : Adding String[] value to a Map


slavkman
Oct 11th, 2006, 03:35 PM
Hello,

I'm trying this in SFC A
Map myModel = new HashMap();
//WORKS
//myModel.put("key1", "123");
//DOES NOT WORK
myModel.put("key1", new String[] {"123", "456"});
return new ModelAndView( new RedirectView(getSuccessView()), myModel );

and then this in SFC B
String numbers[] = ServletRequestUtils.getRequiredStringParameters(re quest, "key1");
getService().getQuery(numbers);

Any trick on getting the String[] to work? I know the service part is set up correctly because if i hardcode the numbers[] array in SFC B then all is well.

Thanks,
S.

slavkman
Oct 12th, 2006, 07:16 PM
I'm probably missing something really simple. Any ideas? I've tried a 100 different ways to get at the Param Array and all I get is a reference.

Thanks,
S.

Arthur Loder
Oct 13th, 2006, 10:18 AM
Hi slavkman,

I think the problem might be that you are calling ServletRequestUtils.getRequiredStringParameters, which retrieves a String[] representing all of the values for the given request parameter. However, the model element is most likely being set as a request attribute.

Relevant API references:

ServletRequest.getAttribute (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getAttribute(java.lang.String) ) vs ServletRequest.getParameterValues (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.S tring)).

Bottom line: you should not try to access elements from the model as request parameters, because that is not how they are stored.

-Arthur Loder

slavkman
Oct 13th, 2006, 02:27 PM
Thanks for the reply Arthur. I went along with your suggestion....

I printed out the attributes names associated with the SFC B's HttpServletRequest and got the following

Attributes name com.ibm.servlet.engine.webapp.dispatch_type
Attributes name org.springframework.web.servlet.DispatcherServlet. THEME_SOURCE
Attributes name org.springframework.web.servlet.DispatcherServlet. CONTEXT
Attributes name org.springframework.web.servlet.DispatcherServlet. LOCALE_RESOLVER
Attributes name org.springframework.web.servlet.HandlerMapping.pat hWithinHandlerMapping
Attributes name org.springframework.web.servlet.DispatcherServlet. THEME_RESOLVER

Would any of these get me access to the model's "key1" value that was sent from SFC A?

Arthur Loder
Oct 13th, 2006, 02:39 PM
Hi slavkman,

Where are you trying to access the model? What does SFC stand for? Could you post more code? Usually, you don't have to access the model attributes explicitly...

-Arthur Loder

slavkman
Oct 13th, 2006, 03:08 PM
Sure, here goes...

I have a SimpleFormController (aka SFC A)

public class FindFormController extends SimpleFormController {

public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws ServletException {

Find find = (Find) command;

Map myModel = new HashMap();
myModel.put("key1", new String[] {"123", "456"});

return new ModelAndView( new RedirectView(getSuccessView()), myModel );

}
}

which redirects to results.htm which is linked to a second SimpleFormController (aka SFC B)

public class ResultsFormController extends SimpleFormController {

protected Object formBackingObject(HttpServletRequest request) throws ServletException {

String numbers[] = ServletRequestUtils.getRequiredStringParameters(re quest, "key1");
Results results = getService().getQuery(numbers);

return results;
}
}

It works if I hard code the numbers[] in ResultsFormController so I know the service call works. It also works if I pass a single string in the model

by switching
myModel.put("key1", new String[] {"123", "456"});
with
myModel.put("key1", "123");

What do you thinK? I know it is a funny way of doing a search but I have my reasons.

Thanks.

tanmay
Oct 13th, 2006, 09:19 PM
Hi slavkman,

I think ServletRequestUtils.getRequiredStringParameters is returning array of array in your case.
just try
getService().getQuery(numbers[0]);
instead of
getService().getQuery(numbers);

It might work.

tanmay
Oct 13th, 2006, 09:45 PM
I think the problem might be that you are calling ServletRequestUtils.getRequiredStringParameters, which retrieves a String[] representing all of the values for the given request parameter. However, the model element is most likely being set as a request attribute.

It might work.

I agress with Arthur

slavkman
Oct 14th, 2006, 09:00 AM
Thanks for the reply.

The Query call works with a String array parameter, I'm using NamedParameterJdbcTemplate.

In other words, this is ok
public class ResultsFormController extends SimpleFormController {

protected Object formBackingObject(HttpServletRequest request) throws ServletException {

String numbers[] = {"123", "456"};
Results results = getService().getQuery(numbers);

return results;
}
}

Has anyone used getRequiredStringParameters to retrieve a String array?

Thanks.