SpringMVC中Controller中方法返回值类型

1、返回ModelAndView

要求前端使用JSP页面,并使用JSTL标签,才可以匹配解析后端返回的数据

后端代码

package com.hxy.controller;


import com.hxy.pojo.Order;
import com.hxy.pojo.Product;
import com.hxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/product/productList.action")
    public ModelAndView itemList(){
        //获取数据列表
        List<Product> list = new ArrayList<Product>();
        list= productService.getProductList();
        //定义返回对象
        ModelAndView modelAndView = new ModelAndView();
        //设置返回数据
        modelAndView.addObject("productList",list);
        //设置返回页面
        modelAndView.setViewName("/static/productList.jsp");
        return modelAndView;
    }

}

前端代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!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>查询商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath}/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${productList}" var="item">
<tr>
	<td>${item.name}</td>
	<td>${item.price}</td>
	<td><fmt:formatDate value="${item.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${item.detail }</td>
	
	<td><a href="${pageContext.request.contextPath}/product/toEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>

2、返回void,即没有返回值

(1)使用request转发页面

                             //要去的页面,并将request与Response传递过去 
request.getRequestDispatcher( "/static/productList.jsp" ).forward( request,response );

(2)使用response页面重定向

//直接写出去哪里
response.sendRedirect( "/static/productList.jsp" );

(3)可以通过Response只是响应结果,比如响应返回JSON数据

3、返回字符串

(1)逻辑视图名

  //返回void
    @RequestMapping(value = "/product/productList4.action")
    public void itemList1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //request.getRequestDispatcher("/static/index.html").forward(request,response);
        response.sendRedirect( "/static/index.html" );
    }    

    @RequestMapping(value = "/product/productList5.action")
    public String itemList2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/product/productList4.action";
    }

(2)redirect重定向


    return "redirect:/productList4.action";  //重定向

(3)forward转发

return "forward:product/productList4.action";   //转发

4、返回Model

要求前端使用JSP页面,并使用JSTL标签,才可以匹配解析后端返回的数据

 @RequestMapping(value = "/product/productList6.action")
    public String itemList3(HttpServletRequest request,
                            HttpServletResponse response,
                            Model model) throws ServletException, IOException {

        List<Product> list = new ArrayList<Product>();
        list= productService.getProductList();
        model.addAttribute("productList",list);
        return "/static/productList.jsp";
    }

总结

ModeAndView:无敌的,带着参数,返回视图的路径,

String:返回视图路径,model带数据,官方推荐,解耦:将视图和数据分开  建议使用

Void:ajax请求,只返回数据,不反悔视图路径,使用response对象       异步请求使用

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名一小卒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值