SpringMV中Controller的返回值

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值