使用JSTL的迭代标签取出集合中的值,怎么写老师?
来源:2-12 自由编程
慕粉1465475474
2020-03-29 21:36:26
package com.imooc.jstl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/month")
public class MonthServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MonthServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建Set集合
Set set = new HashSet();
//将5个月份添加到集合中
set.add("Janurary");
set.add("Feburary");
set.add("March");
set.add("April");
set.add("May");
//将集合存储到request中
request.setAttribute("months", set);
request.getRequestDispatcher("/test212").forward(request, response);
}
}
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset=utf-8>
<title>月份</title>
</head>
<body>
<c:forEach items="${requestScope.months }" var="m" varStatus="idx">
</c:forEach>
</body>
</html>
1回答
同学你好,建议在MonthServlet中跳转jsp文件中,并建议同学可通过forEach进行实现展示内容。如下所示:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题