天天看點

SpringMVC轉發和重定向的差別是什麼

轉發和重定向

視圖解析器

<!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- 字首 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 字尾 -->
        <property name="suffix" value=".jsp" />
    </bean>      

ResultSpringMVC2

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ResultSpringMVC2 {
  @RequestMapping("/rsm2/t1")
  public String test1(){
    //轉發
    return "test";    //這個路徑可以從視圖解析器擷取
  }
  @RequestMapping("/rsm2/t2")
  public String test2(){
    //重定向
    **return "redirect:/index.jsp";**  //這個路徑需要是完整的路徑
  }
}      

作者:​​靠譜楊​​​

我可能不是天才,但我會努力成為人才。

更多日常分享盡在我的VX公衆号:小楊的挨踢IT生活