Spring RedirectAttributes - spring

How can we use spring RedirectAttributes to add a attribute with same name and multiple values, like if I have HTTP request parameter say place=london&place=paris, and I want to redirect these "place" params in a redirectAttribute.
redirectAttribute.addAttribute("place",places??)
I don't want to use flash attributes.
Is it possible?

I'm really frustrated that there doesn't appear to be a cleaner solution, but I faced the same problem and ended up doing:
((HashMap<String, Object>) redirectAttrs).putIfAbsent("place",
Arrays.asList("london", "paris"));
This works because it bypasses the way that RedirectAttributes normally converts everything to a String when you add it, since they forgot to override the putIfAbsent method...
It works (for now), but I certainly don't recommend it.

You can use the mergeAttributes method:
String[] attrArray = {"london", "paris"};
Map<String, String[]> attrMap = new HashMap<>();
attrMap.put("place", attrArray);
redirectAttrs.mergeAttributes(attrMap);
This will create a URL with request parameters like this: place=london%2Cparis (where %2C is the ASCII keycode in hexadecimal for a comma), which is equivalent to place=london&place=paris.

Related

Is thera any ways to use html template in controller and then add it to a new ModelAndView?

I have one html template for multiple get requests. However, each request only have a piece of html code that is different from others. The code is a little complicated that does not proper to write with Java strings. So I try to make a template html file, then I will use Java to read the html file and replace the variables with new values. I don't know if there is some way to write values into a html file, and then read the html file to a String variable in Java, and then use ModelAndView to add the html String variable to views. The sample code is as follows:
// sample code in controller
ModelAndView mav = new ModelAndView("test.html");
String htmlTemplate = FileUtils.readToString(filepath);
String username = "xx";
htmlTemplate.replace("$username", username);
mav.addObject("usernameHTML", htmlTemplate);
return mav;
I don't know whether this way of coding is proper. Or how to solve this problem?

Spring Rest Service GET with parentheses

I have a Spring Rest Service like this:
#RequestMapping(value = "/banks", method = RequestMethod.GET)
public List<String> getBanks( #RequestParam(value="name" , required=true) String name) {
...
}
The name must allows special character like parentheses, but the problem is that when i put parentheses on the param, y receive the ASCCI code like #040# on the "name" parameter.
I thought in use #RequestBody with a wrap filter like a posible solution, but the method must change to POST to support the wrap in the Request Body and the api design going to be bad.
So, someone have a solition for support parentheses on the param of a GET Rest Service?

Best Practice of reading text file in spring mvc

What is the best way to read HTML content from a text file and display the content in JSP? I have placed the text file in the resource folder using spring mvc 3. Normally I do this kind of stuff with Apache commons in struts but believe spring must have provided some utility for this.
I am thinking about reading it in a tag file. What utility should i use for that?
Ignoring the question of why you would want to do this. The return value from a Spring MVC controller method is usually used to resolve a view. The resolved view then becomes the response body. However, you can use the #ResponseBody annotation to make the raw return value the response body.
You can do it in the #RequestMapping method. The Key is the return type ModelAndView.
You read your text file and get the html that you need, after this, adding the html to the model and then return the a new ModelAndView Object.
Here's a sample:
#RequestMapping(value = "siteWithResHtml", method = RequestMethod.GET)
public ModelAndView loadSiteWithResHtml(Model model)
{
String resourceHtml;
// do your stuff for reading file and assign it to the String
model.addAttribute("resource_Html", resourceHtml);
return new ModelAndView("yourJSP", "model", model);
}
and in the jsp you can read the value from the model that you forwarded to the jsp, like this:
<div>${model.resource_Html}</div>
please take notice that the names are match.

RequestMapping doesn't redirect

#RequestMapping(value = "/{Id}", method = RequestMethod.GET)
public String searchCity( #PathVariable ("Id") Long Id) {
mymethod.something(id);
return "successpage";
}
When I'm trying to write some number it print me Eror 404 resource is not available, but when I write {Id} ("7DId7D" smthing like this) it redirects me to succespage, What is the problem? Help me please...
The information you provided conflicts with known behavior of Spring MVC
http://localhost:8080/MyCountryProject/7 should maps to searchCity fine
http://localhost:8080/MyCountryProject/%7Bld%7D should not even map to searchCity
I would check following to further isolate the problem:
Are you sure you're testing against the right controller? If your controller has #RequestMapping("myController") then your URL would be http://localhost:8080/MyCountryProject/MyController/7
Are you sure you're posting with the correct HTTP method? If you're HTTP form is POST, it wouldn't map to searchCity method
The conversion from string into Long is done by Spring MVC builtin property editor, did you install your own property editor? If so debug this

passing data in querystring when using tiles

I am using tiles2 and spring in my project. When i am redirecting from spring controller to a jsp(the jsp page is mapped in tiles.xml file) page using query string like:
return "showRes.jsp?subSucc=ok";
it shows me:
javax.servlet.ServletException: Could not resolve view with name 'showRes.jsp?subSucc=ok'
I think this is wrong way to passing data using query string.
Please tell me how can i do this.
Thanks
Shams
The Problem is that return "showRes.jsp?subSucc=ok"; statment should return the name of a jsp and it is NOT a URL.
The normal Spring way to pass values is a jsp is to use a Model Map (of course there are some other ways, but this is the easysest to describe one).
Have a look at the ModelAndView and Model class. Create an instance of it, set the view name and add your parameter, and then return it instead of the String.
Model model = new Model();
model.addAttribute("subSucc","ok");
ModelAndView modelAndView = new ModelAndView("showRes.jsp", model);
//may without ".jsp" postfix - this depends on your configuration
return modelAndView;

Resources