c:forEach无法显示信息的可能原因以及需要注意的地方

1.注意的地方

1.第一点

记得在JSP页面加上
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
这句话很重要,如果没有,那么JSP页面就无法显示出信息
记得导入包
在这里插入图片描述

2.第二点

正确的使用数组
数组的长度是一定的,如果要传入未知长度的数据,记得使用List,使用List要记得初始化,即
List < String> a = new ArrayList(); 不能单纯的写List< String> a;,添加数组元素的时候要使用add方法

2.例子

Servlet

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowFiles
 */
@WebServlet("/ShowFiles")
public class ShowFiles extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowFiles() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		List<String> a = new ArrayList();
		String[] test={"a","b","c"};
		int j=0;String b;
		String path = "G:\\rsdownload"; // 路径
	        File f = new File(path);
	        if (!f.exists()) {
	            System.out.println(path + " not exists");
	            return;
	        }
	 
	        File fa[] = f.listFiles();
	        for (int i = 0; i < fa.length; i++) {
	            File fs = fa[i];
	            if (fs.isDirectory()) {
	                //System.out.println(fs.getName()+" "+i);
	                b=fs.getName();
	                a.add(b);
	               
	            } else {
	                //System.out.println(fs.getName());
	                b=fs.getName();
	                a.add(b);
	            }
	        }
	        request.setAttribute("File", a);
	        request.setAttribute("test", test);
	        System.out.println(a);
	        request.getRequestDispatcher("listFiles.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

JSP

<%@ page language="java" 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!--<%request.getAttribute("File"); %>  -->

     
					<table class="table">
						<tr class="table_header">
							<td>
								File Name
							</td>
							<td>
								Downlink
							</td>
							
						</tr>
						
						<c:forEach items="${File }" var="File" >
						<td>
						 ${File}<br>
                         
						</td>
						<td>
                        www.google.com
                        </td>
                        <tr>
                         </c:forEach>
					</table>
</body>
</html>

运行结果
在这里插入图片描述
对于从Servlet到JSP的中文乱码问题,我们要在doGet方法的第一句加上response.setCharacterEncoding(“utf-8”);这里是要么把response.getWriter().append("Served at: ").append(request.getContextPath());这句话删掉,要么就把response.setCharacterEncoding(“utf-8”);写在它的前面,而不要写在它的后面就行了。
在这里插入图片描述

3.参考

https://blog.csdn.net/qq_34646449/article/details/76146659
https://zhidao.baidu.com/question/394152982.html
https://zhidao.baidu.com/question/1447371832991262740.html
https://blog.csdn.net/eminem2011/article/details/7065860
http://ask.zol.com.cn/x/5321764.html
https://blog.csdn.net/x763795151/article/details/52472014

  • 17
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值