简体   繁体   中英

Internal forwarding from one controller method to another (Spring-MVC)

I am new to spring so not sure how i can do this.I have to handle 2 use case by using single controller method.

Method is already in place so i can not change the signature of it as it can break all other functionalists, here is the method signature

@RequestMapping(value = "/edit-address", method = RequestMethod.POST)
    public @ResponseBody JsonResponse editAddress()

JsonResponse is a custom object with following signature

private String status;
private Object result;
private String steps;

There is one more requirement where we need to validate this address from third part and based on the return results need to show pop-up. I can easily show popu-up if the method signature was not a custom object, but now not sure how i can do this

is there a way to achieve any of the followwing

  1. To send JSP content back to the view in place of JSON object from this method.
  2. To internally forward control to another method in controller which can send back view to the UI

How about just composing this controller in another controller that you can control and delegating the call internally to this controller, this way:

@Controller
public class WrapperController{
    @Autowired JsonResponseController jsonResponseController;

    @RequestMapping("/custom_jspview")
    public String customRequestMapping(..., Model model){
        JsonResponse jsonResponse = this.jsonResponseController.editAddress();
        model.addAttribute("jsonresponse", "jsonResponse");
        return "myview";
    }

    @RequestMapping("/redirectView")
    public String redirectView(){
         return "redirect:/custom_jspview";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM