Detail Java's method for redirecting from background (redirect) to another project

  • 2020-07-21 08:01:08
  • OfStack

(1) Jump through ModelAndView


@RequestMapping("alipayforward") 
  public ModelAndView alipayforward(HttpServletRequest req, HttpServletResponse resp) throws Exception { 
    String contNo =req.getParameter("contNo"); 
    logger.info(" access /downloadRequestElecCont.action"); 
    String url = "redirect:http://baidu.com/downloadRequestElecCont.action?contNo="+contNo;  
    return new ModelAndView(url); 
  } 

(2) Jump through HttpServletResponse


@RequestMapping("alipayforward/{contNo}") 
  public void alipayforward(@PathVariable("contNo") String contNo, HttpServletRequest req, HttpServletResponse resp) throws Exception { 
    //String contNo =req.getParameter("contNo"); // Insurance policy no.  
    logger.info(" access /downloadRequestElecCont.action"); 
    resp.sendRedirect("http://baidu.com/downloadRequestElecCont.action?contNo="+contNo); 
  } 

(3) Return an String type jump via redirect. Note that this method does not allow the Spring controller to annotate with @RestController, since @RestController is equivalent to all methods in the class annotated with @ResponseBody. Instead of returning a view, these methods return an json object, which simply prints a string on the page without jumping. The controller can be annotated with @Controller


@RequestMapping("alipayforward") 
  public String alipayforward(@RequestParam("contNo") String contNo, HttpServletRequest req, HttpServletResponse resp) throws Exception { 
    //String contNo =req.getParameter("contNo"); // Insurance policy no.  
    logger.info(" access /downloadRequestElecCont.action"); 
    return "redirect:http://baidu.com/downloadRequestElecCont.action?contNo="+contNo; 
  } 

The following is about the transmission of parameters

The redirection is in get mode and can be encapsulated in map or modelMap if more parameters are passed


@RequestMapping(params = "action=alipayforward") 
  public String alipayforward(Map modelMap){ 
    modelMap.put("userName", " Ha ha "); 
    modelMap.put("password", "123456"); 
    modelMap.put("age", "25"); 
    return "redirect:http://localhost:8088/era/user/alipayforward4?modelMap="+modelMap; 
  } 

The other item is received as an entity class object


@RequestMapping("alipayforward4") 
  public void alipayforward4(User user, HttpServletRequest req) throws Exception { 
    System.out.println(user.getPassword()); 
    String modelMap = req.getParameter("modelMap"); 
    System.out.println(modelMap); 
  } 

Related articles: