转发和重定向

Exisi 2022-06-28 14:43:52
Categories: Tags:

关键字

描述

forward:

视图转发,获取视图参数${value}

可访问WEB-INF

redirect:

视图重定向,参数转换为get方式的明文链接,获取视图参数${param.value}

不可访问WEB-INF

 

语法

forward:/path/index.jsp

示例

@Controller

public class MainController {

 

    @RequestMapping(value="forward")

    public ModelAndView doForward(){

ModelAndView mav = new ModelAndView();

mav.setObject("name","zhangsan");

//转发到WEB-INF外的目录

mav.setViewName("forward:/hello.jsp")

          return mav;

    }

 

    @RequestMapping(value="redirect")

    public ModelAndView doRedirect(){

ModelAndView mav = new ModelAndView();

mav.addObject("name","zhangsan");

mav.setViewName("redirect:/show.jsp")

            return mav;

    }

}

 

<html>

    <head>

      <title></title>

    </head>

    <body>

      <p>姓名:${name}</p>

    </body>

</html>

 

<html>

    <head>

      <title></title>

    </head>

    <body>

      <p>姓名:${param.name}</p>

     <!-- <p>姓名:<%request.getParameter("name")%></p> -->

    </body>

</html>