Hi I am new to Spring MVC ,I want to call method from one controller to another controller ,how can I do that .please check my code below
@Controller
@RequestMapping(value="/getUser")
@ResponseBody
public User getUser()
{
User u = new User();
//Here my dao method is activated and I wil get some userobject
return u;
}
@Controller
@RequestMapping(value="/updatePSWD")
@ResponseBody
public String updatePswd()
{
here I want to call above controller method and
I want to update that user password here.
how can I do that
return "";
}
any one help me .
Yes, you can call a method of another controller. The controller is also a simple class.
Spring REST Controller TestGo to URL https://localhost:8080/Spring-Controller/rest and you should get following output. Now let's provide the name parameter value in the URL, go to https://localhost:8080/Spring-Controller/rest/person/get?name=Pankaj and you will get following JSON response.
@Controller public class MainController { @Autowired OtherController otherController; @RequestMapping("/myurl") public String handleMyURL(Model model) { otherController. doStuff(); return ...; } } @Controller public class OtherController { @RequestMapping("/doStuff") public String doStuff(Model model) { ... } }
Can do like this:
@Autowired
private MyOtherController otherController;
@RequestMapping(value = "/...", method = ...)
@ResponseBody
public String post(@PathVariable String userId, HttpServletRequest request) {
return otherController.post(userId, request);
}
You never have to put business logic into the controller, and less business logic related with database, the transactionals class/methods should be in the service layer. But if you need to redirect to another controller method use redirect
@RequestMapping(value="/updatePSWD")
@ResponseBody
public String updatePswd()
{
return "redirect:/getUser.do";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With