Controller的返回值
环境用得就是上一篇的环境:我们现在具体来看一下它有哪些返回值:
ModelAndView、String、void
看代码:
package com.RequestMapping.Controller;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.RequestMapping.Service.IEmpService;
import com.RequestMapping.entity.Emp;
/**
* 控制器:处理器请求,响应结果
*/
@Controller
@RequestMapping("/emp")
public class EmpController {
@Resource(name="empService")
private IEmpService empService;
/*
* 1、 返回ModelAndView
*/
@RequestMapping(value="/get",method = {RequestMethod.GET,RequestMethod.POST})
public ModelAndView getEmps()
{
ModelAndView mav = new ModelAndView();
//1.调用service方法,查询所有雇员信息
List<Emp> empList= empService.selectEmp();
//2.保存到作用域
mav.addObject("empList", empList);
//3.指定跳转的路径
/*
因为我们在spring-mvc.xml中的视图解析器中配置了前缀与后缀,所以不需要写
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
*/
mav.setViewName("empQuery");
return mav;
}
/*
* 2、返回string:返回要跳转的路径(转发)
*/
@RequestMapping("/get2")
public String getEmps2(HttpServletRequest request,HttpServletResponse response)
{
//1.调用service方法,查询所有雇员信息
List<Emp> empList= empService.selectEmp();
//2.保存到作用域
request.setAttribute("empList", empList);
//3.指定跳转的路径
return "empQuery";
}
/*
* 2.1、返回string:如果需要跳转到其他后缀的页面比如说.html,
* 当然我就不写HTML页面了,就用加了前缀与后缀的jsp页面表示,
* 也就是回到了没在视图解析器中配置前后缀的情况,一样的。
* 在路径前加一个forward:"路径";(转发)
*/
@RequestMapping("/get3")
public String getEmps3(HttpServletRequest request,HttpServletResponse response)
{
//1.调用service方法,查询所有雇员信息
List<Emp> empList= empService.selectEmp();
//2.保存到作用域
request.setAttribute("empList", empList);
//3.指定跳转的路径
return "forward:/empQuery.jsp";//不会拼接前后缀
}
/*
* 2.2、返回string:使用重定向(Redirect)
*/
@RequestMapping("/get4")
public String getEmps4(HttpServletRequest request,HttpServletResponse response)
{
//1.调用service方法,查询所有雇员信息
List<Emp> empList= empService.selectEmp();
//2.保存到作用域
HttpSession session= request.getSession();
session.setAttribute("empList", empList);
//3.指定跳转的路径
return "redirect:/empQuery.jsp";
}
/*
* 3、void:无返回值
*/
@RequestMapping("/get5")
public void getEmps5(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
//1.调用service方法,查询所有雇员信息
List<Emp> empList= empService.selectEmp();
//2.保存到作用域
HttpSession session= request.getSession();
session.setAttribute("empList", empList);
//3.指定跳转的路径
request.getRequestDispatcher("/empQuery.jsp").forward(request, response);
}
}
测试代码:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="emp/get.action">点击获取所有雇员信息-------返回ModelAndView</a><br><br>
<a href="emp/get2.action">点击获取所有雇员信息-------返回String(转发RequestDispatcher)</a><br><br>
<a href="emp/get3.action">点击获取所有雇员信息-------返回String,使用forward</a><br><br>
<a href="emp/get4.action">点击获取所有雇员信息-------返回String使用重定向Redirect</a><br><br>
<a href="emp/get5.action">点击获取所有雇员信息-------返回void</a><br><br>
</body>
</html>
本文详细解析了Spring MVC框架中控制器的各种返回值类型,包括ModelAndView、String和void,以及它们在处理请求和响应过程中的应用。通过实例展示了如何使用这些返回值进行页面跳转和数据传递。

1699

被折叠的 条评论
为什么被折叠?



