如何在 Spring MVC 中将一个控制器调用到另一个控制器 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24949846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:50:34  来源:igfitidea点击:

How to call one controller to another controller URL in Spring MVC?

springspring-mvc

提问by user2963481

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

嗨,我是 Spring MVC 的新手,我想从一个控制器调用方法到另一个控制器,我该怎么做。请检查下面的代码

@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 .

任何人帮助我。

回答by paul

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

回答by Pavel Evstigneev

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

回答by Raedwald

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

控制器类和其他类一样是一个 Java 类。尽管 Spring 为您提供了巧妙的魔法,使用反射来检查注释,但您的代码可以像普通的 Java 代码一样调用方法:

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

回答by Oomph Fortuity

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

这里不需要添加@reponseBody注释作为您重定向到另一个控制器您的代码看起来像

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

}

回答by Thuy Nguyen

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

您应该将方法 getUser 放在服务中(例如 UserService 类)。

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

在 getUser 控制器中,调用 Service 中的 getUser 方法来获取 User

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

同样,在 updatePswd 控制器中,您也调用 Service 中的 getUser 方法