springMVC的转发和重定向及Controller参数

来源:互联网 发布:阿里云学生服务器 编辑:程序博客网 时间:2024/05/20 03:44

在springMVC中可以通过返回逻辑视图名进行转发和重定向:

例如重定向在同一个Controller中的方法中,可以在返回结果的时候使用“return "redirect:url"”

<pre class="java" name="code">@RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST})    public String getInfos(){               order.ListOrder();        return "redirect:redire";    }    @RequestMapping("/redire")    public String redirect(){        return "a";    }
 

转发可以使用return forward:url

 @RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST})    public String getInfos(){        order.ListOrder();        return "forward:redire";    }    @RequestMapping("/redire")    public String redirect(){        return "a";    }

 

controller方法形参上可以定义requestresponse,使用requestresponse指定响应结果:

1、使用request转向页面,如下:

request.getRequestDispatcher("页面路径").forward(request,response);

2、也可以通过response页面重定向:

response.sendRedirect("url")

 public void getInfos(HttpServletRequest request,HttpServletResponse response){        order.ListOrder();        //return "forward:redire";        try {            response.sendRedirect("redire");        } catch (IOException e) {            e.printStackTrace();        }    }    @RequestMapping("/redire")    public String redirect(){        return "a";    }



 

 

0 0
原创粉丝点击