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";
}