欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springMvc請(qǐng)求的跳轉(zhuǎn)和傳值的方法

 更新時(shí)間:2017年02月14日 16:32:55   作者:liuconglin  
本篇文章主要介紹了springMvc請(qǐng)求的跳轉(zhuǎn)和傳值的方法,這里整理了幾種跳轉(zhuǎn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

forword跳轉(zhuǎn)頁(yè)面的三種方式:

1.使用serlvet

/**
   * 使用forward跳轉(zhuǎn),傳遞基本類型參數(shù)到頁(yè)面
   *   注意:
   *     1.使用servlet原生API Request作用域
   *     
   */
  @RequestMapping("/test")
  public String test(HttpServletRequest request,HttpServletResponse response){
    String name = "張小三";
    request.setAttribute("name",name);
    return "/back/attr";
  }

2.使用Model對(duì)象

/**
   * 使用forward跳轉(zhuǎn),傳遞基本類型參數(shù)到頁(yè)面
   *   注意:
   *     1.使用springmvc 封裝好的Model對(duì)象(底層就是request作用域)
   */
  @RequestMapping("/test1")
  public String test1(Model model){
    String name = "張小四";
    model.addAttribute("name", name);
    return "back/attr";
    
  }

3.使用ModelAndView

/**
   * 使用modelAndView
   *   注意事項(xiàng)
   *     modelAndView對(duì)象中的數(shù)據(jù)只能被ModelAndView對(duì)象的視圖獲取
   */
  @RequestMapping("/test2")
  public ModelAndView test2(ModelAndView modelAndView){
    String name = "張小五";
    modelAndView.setViewName("back/attr");
    modelAndView.addObject("name", name);
    return modelAndView;
     
  }

當(dāng)然也可以通過(guò)new 一個(gè)ModelAndView對(duì)象來(lái)實(shí)現(xiàn)

@RequestMapping("/test3")
  public ModelAndView test3(){
    String name = "張小六";
    return new ModelAndView("back/attr", "name", name);
  }

forword跳轉(zhuǎn)到Controller中的方法:

跳轉(zhuǎn)到相同類中的方法

/**
   * 使用forword跳轉(zhuǎn)到相同類中的某一方法
   * 注意:
   *     1.不需要加上類上的@RequestMapping的值
   */
  @RequestMapping("/test00")
  public String test00(){
    return "forward:test1";
  }

跳轉(zhuǎn)到不同類中的方法:

/**
   * 使用forword跳轉(zhuǎn)到不同類中的某一方法
   * 注意:
   *     1.需要加上類上的@RequestMapping的值:比如 :/hello
   */
  @RequestMapping("/test01")
  public String test01(){
    return "forward:/hello/test";
  }

redirect跳轉(zhuǎn)到頁(yè)面:

 使用servlet

/**
   * 使用redirect跳轉(zhuǎn) 向頁(yè)面?zhèn)鬟f數(shù)據(jù)
   *     1.使用Servlet原生API Session ServletContext
   */
  
  @RequestMapping("/test4")
  public String test4(HttpServletRequest request,HttpSession session){
    String name = "張曉霞";
    session.setAttribute("name", name);
    return "redirect:/back/attr.jsp";
  }

使用ModelAndView

/**
   * 使用redirect跳轉(zhuǎn) 向頁(yè)面?zhèn)鬟f數(shù)據(jù)
   *     1..使用ModelAndView對(duì)象 modelAndView對(duì)象會(huì)把model中的數(shù)據(jù)以?形式拼接到地址欄后 可以使用${param.key}接受
   */
  @RequestMapping("/test5")
  public ModelAndView test5(){
    return new ModelAndView("redirect:/back/attr.jsp","name","小張張");
  }

redirect跳轉(zhuǎn)到Controller中的方法:

跳轉(zhuǎn)到同類和不同類的方法都需要加上類上的@RequestMapping,就不粘出測(cè)試代碼了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論