9

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 .

1
  • 2
    you shouldn't call another controller method, move the logic to a service class Commented Jul 25, 2014 at 7:21

5 Answers 5

15

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);
}
Sign up to request clarification or add additional context in comments.

Comments

10

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";
}

1 Comment

How to call in case getUser(String destURL) method takes argument?
1

A controller class is a Java class like any other. Although Spring does clever magic for you, using reflection to examine the annotations, your code can call methods just as normal Java code:

 public String updatePasswd()
 {
    User u = getUser();
    // manipulate u here
    return u;
 }

Comments

0

You should place method getUser in a service (example UserService class) .

In the getUser controller, you call method getUser in the Service to get the User

Similarly, in the updatePswd controller, you call method getUser in the Service ,too

Comments

0

Here no need to add @reponseBody annotation as your redirecting to another controller Your code will look like

@Controller
class ControlloerClass{

        @RequestMapping(value="/getUser",method = RequestMethod.GET)
        @ResponseBody
        public User getUser(){
            User u = new User();
            //Here my dao method is activated and I wil get some userobject
            return u;
        }

        @RequestMapping(value="/updatePSWD",method = RequestMethod.GET)
        public String updatePswd(){
           //update your user password
           return "redirect:/getUser";
        }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.