方式一:ModelAndView
设置 ModelAndView 对象,根据 view 的名称和视图解析器跳到指定的页面
页面:视图解析器前缀 + viewName + 视图解析器后缀
视图解析器配置:
<!-- 视图解析器 : 模板引擎Thymeleaf Freemarker-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
对应的 controller 类:
public class FirstController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
//业务代码
String result = "Hello,SpringMVC";
modelAndView.addObject("msg",result);
//视图跳转(要跳转的页面,这里跳转到test.jsp页面)
modelAndView.setViewName("test");
return modelAndView;
}
}
方式二:ServletAPI
- 通过 HttpServletResponse 进行输出
- 通过 HttpServletResponse 实现重定向
- 通过 HttpServletRequest 实现转发
package com.yuhuofei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Description
* @ClassName TestServletApi
* @Author yuhuofei
* @Date 2022/7/16 17:10
* @Version 1.0
*/
@Controller
public class TestServletApi {
//打印输出
@RequestMapping("/result/print")
public void print(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().println("Use Servlet API");
}
//请求重定向
@RequestMapping("/result/redirect")
public void redirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.jsp");
}
//请求转发
@RequestMapping("/result/forward")
public void forward(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setAttribute("message", "请求转发");
request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request, response);
}
}
方式三:SprngMVC
无需视图解析器,将视图解析器注释掉
package com.yuhuofei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description
* @ClassName TestSpringMvc
* @Author yuhuofei
* @Date 2022/7/16 17:27
* @Version 1.0
*/
@Controller
public class TestSpringMvc {
//转发
@RequestMapping("/result/forward")
public String forward(){
//方式一
return "/index.jsp";
//方式二
//return "forward:/index.jsp";
}
//重定向
@RequestMapping("/result/redirect")
public String redirect(){
return "redirect:/index.jsp";
}
}
使用视图解析器
package com.yuhuofei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description
* @ClassName TestSpringMvc
* @Author yuhuofei
* @Date 2022/7/16 17:27
* @Version 1.0
*/
@Controller
public class TestSpringMvc {
//转发
@RequestMapping("/result/forward")
public String forward(){
return "index";
}
//重定向
@RequestMapping("/result/redirect")
public String redirect(){
return "redirect:/index.jsp";
}
}
重定向,不需要视图解析器,本质是重新请求一个新的地址或者接口。