How to change url after spring controller invocation? - java

I have approximately following controllers:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public String foo(RedirectAttributes redirectAttributes, Model model) {
//logic
return bar(model);
}
#RequestMapping(value = "bar", method = RequestMethod.GET)
public String bar (Model model) {
model.addAttribute("value","magicValue")
return "myJsp";
}
my aim that after /foo invocation url was changed with bar
method bar shouldn't be broken.
Is it possible?

In method foo you can do:
return new ModelAndView("redirect:bar", modelName, model);
But this will add additional request to a server. It will also put the model values in the URL which is not always desirable

Related

Java - Pass multiple parameters to ajax post

I have this simple controller from ajax request. It works but I wanted to return many stuff, not only List TestFlow.getFlow(flowName);
#RequestMapping(value = "/execute-flow/getFlow" , method = RequestMethod.POST)
public #ResponseBody List<String> getFlow(#RequestParam("flowName") String flowName) {
return TestFlow.getFlow(flowName);
}
Can I return multiple things to the ajax post?
For example:
#RequestMapping(value = "/execute-flow/getFlow" , method = RequestMethod.POST)
public #ResponseBody List<String> getFlow(#RequestParam("flowName") String flowName) {
return TestFlow.getFlow(flowName);
return TestFlow.getInputs(flowName);
return TestFlow.getCode(flowName);
}
Not sure what exactly you're after, but
return Arrays.asList(
TestFlow.getFlow(flowName),
TestFlow.getInputs(flowName),
TestFlow.getCode(flowName));
should work, provided that all of these are of the same type (String).

Return a ModelAndView with a List Object Simultaneously

I have a List of Managers that I need to return in my #Controller method. I also have a User form view that I need to return simultaneously. managerList is returned from a previous #Controller. I may have been staring at this screen to long, it may not even make sense to do so, but can this be done?
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
return new ModelAndView("userForm");
}
Output would be a blank user form with a List of managers that say would be loaded into a select input. Any ideas?
Thanks much
You can use use public ModelAndView(String viewName, Map<String, ?> model).In model you can put your list.
You can use the model map of the ModelAndView Object
try the below code
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
model.addObject("managerList", managerList);
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}

Spring #SessionAttributes returns null

Im trying to use #SessionAttributes in my controller class as follows:
#Controller
#SessionAttributes({"calMethods", "taxList"})
#RequestMapping("/secure")
public class reportController extends BaseController {
//..
#ModelAttribute("taxList")
public List<multiCalDto> getTaxList() {
return new ArrayList<multiCalDto>();
}
//....
#RequestMapping(value = "/confirmCal.html", method = RequestMethod.GET)
public ModelAndView launchconfirmCal(HttpServletRequest request, #RequestParam("seqNo") String seqNo) {
...........
ModelAndView modelAndView = new ModelAndView("confirmCalView");
modelAndView.addObject("taxList", calBean.getTaxList());
return modelAndView;
}
#RequestMapping(value = "/executeCalPay.html", method = RequestMethod.POST)
public ModelAndView executeCalPay(HttpServletRequest request, #ModelAttribute("taxList") List<multiCalDto> taxList) {
// ............
ModelAndView modelAndView = new ModelAndView("calView");
System.out.println("taxList -- "+taxList);
return modelAndView;
}
}
I added taxList in launchconfirmCal() and trying to access the same in executeCalPay().
I tried to print taxList before adding to modelAttribute and the size is 12 and when I retireve in executeCalPay() it shows null.
I am not changing its value in JSP.
Remove or comment out this method and retry
#ModelAttribute("taxList")
public List<multiCalDto> getTaxList() {
return new ArrayList<multiCalDto>();
}
#ModelAttribute annotated methods are called before ALL request mapping methods, so it resets your taxList before the post method is called

Use of getFlashAttributes() in Spring's RedirectAttributes

In order to access the redirect attributes in the redirected method, we utilize the model's map, like this :
#Controller
#RequestMapping("/foo")
public class FooController {
#RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model map) {
String some = (String) map.asMap().get("some");
}
#RequestMapping(value = "/bar", method = RequestMethod.POST)
public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
redirectAttrs.addFlashAttributes("some", "thing");
return new ModelAndView().setViewName("redirect:/foo/bar");
}
}
But, why can't we access them in this way :
#RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(RedirectAttributes redAttr) {
String some = redAttr.getFlashAttributes().get("some");
}
If the only purpose of adding flashAttributes is that they become available to the model in the redirected method, what's the purpose of getFlashAttributes() ?
RedirectAttributes are for setting flash attributes before redirection. They are merged into model after the redirection so there is no reason to access them again via RedirectAttributes again as you have suggested.
Being able to work with the attributes just like with a map might be useful. You can check what have you set (containsKey, isEmpty, ...). However the use of the wildcard generic parameter Map<String, ?> getFlashAttributes() prevents writing into map and it is strange why they have used it instead of a plain Object parameter.

.getJSON 404 Error with ModelAndView

I am trying to retrieve some JSON data in my javascript by making a call to the controller. The controller returns a MappingJacksonJsonView ModelandView, but the .getJSON is always reporting a 404 at .../handhygiene.json.
Is there a problem with the way I am returning the ModelandView from the controller?
Controller
#RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public ModelAndView getHandHygienePageAsync(
#PathVariable(value = "room") String roomCode) {
ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
mav.getModelMap().addAttribute(blahblahblah); //adds some attributes
...
return mav;
}
Javascript
var currentURL = window.location;
$.getJSON(currentURL + ".json",
function(data) {
... //does stuff with data
}
If you're trying to get only an JSON object from Ajax request, you need to add #ResponseBody to your method, and make you result object as return from your method.
The #ResponseBody tells to Spring that he need to serialize your object to return to the client as content-type. By default, Spring uses JSON Serialization. ModelAndView will try to return an .JSP page. Maybe you don't have this jsp page on your resources so, the server return 404 error.
I Think this code should work for you:
#RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public #ResponseBody Room getHandHygienePageAsync(#PathVariable(value = "room") String roomCode) {
Room room = myService.findRoomByRoomCode(roomCode);
return room;
}
I'm assuming you're using the Room as your result object, but it may be another object or ArrayList, for example.
You cant take a look here for Spring example, and here for example and configuration.

Categories