详解Java从后台重定向(redirect)到另一个项目的方法
程序员文章站
2024-02-29 19:32:34
(1)通过modelandview跳转
@requestmapping("alipayforward")
public modelandview alip...
(1)通过modelandview跳转
@requestmapping("alipayforward") public modelandview alipayforward(httpservletrequest req, httpservletresponse resp) throws exception { string contno =req.getparameter("contno"); logger.info("访问/downloadrequesteleccont.action"); string url = "redirect:http://baidu.com/downloadrequesteleccont.action?contno="+contno; return new modelandview(url); }
(2)通过httpservletresponse跳转
@requestmapping("alipayforward/{contno}") public void alipayforward(@pathvariable("contno") string contno, httpservletrequest req, httpservletresponse resp) throws exception { //string contno =req.getparameter("contno"); //保单号 logger.info("访问/downloadrequesteleccont.action"); resp.sendredirect("http://baidu.com/downloadrequesteleccont.action?contno="+contno); }
(3)通过redirect返回string类型跳转,注意这种方法不允许spring控制器用@restcontroller注解,因为@restcontroller相当于类中的所有方法都标注了@responsebody,这些方法不会返回一个视图,而是返回一个json对象,这样的话只是在页面上打印出字符串,而不跳转。控制器用@controller注解即可
@requestmapping("alipayforward") public string alipayforward(@requestparam("contno") string contno, httpservletrequest req, httpservletresponse resp) throws exception { //string contno =req.getparameter("contno"); //保单号 logger.info("访问/downloadrequesteleccont.action"); return "redirect:http://baidu.com/downloadrequesteleccont.action?contno="+contno; }
下面是关于传参问题
重定向传参为get方式,如果传参数较多,可以封装到map或modelmap中
@requestmapping(params = "action=alipayforward") public string alipayforward(map modelmap){ modelmap.put("username", "呵呵"); modelmap.put("password", "123456"); modelmap.put("age", "25"); return "redirect:http://localhost:8088/era/user/alipayforward4?modelmap="+modelmap; }
另一个项目用实体类对象接收
@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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。