qq_43556827 2021-04-14 15:26 采纳率: 100%
浏览 136
已采纳

在jsp-form表单中使用c:forEach,页面初次加载不显示数据

每次都需要点submit才可以显示数据。

我感觉是因为页面需要请求才能进行反应,然后我jsp初次加载是没有请求的。

该怎么解决??

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

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

import dao.PersonDao;
import dao.PersonDaoImpl;
import entity.Person;

@WebServlet("/Test01Servlet")
public class Test01Servlet extends HttpServlet {

    public Test01Servlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String z = request.getParameter("mm");
		if("showall".equals(z)){
			List<Person> l = new PersonDaoImpl().showAll();
			request.setAttribute("plist", l);
			request.getRequestDispatcher("test01.jsp").forward(request, response);
		}
	}

}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.*" %>
<%@ page import="entity.*" %>
<!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>Test01</title>
</head>
<body>
	<form action="Test01Servlet?mm=showall" method="post">
		<table border="1">
			<tr>
				<th>name</th>
				<th>age</th>
				<th>phone</th>
				<th>city</th>
			</tr>
			<c:forEach items="${plist }" var="p">
				<tr>
					<td>${p.name }</td>
					<td>${p.age }</td>
					<td>${p.phones }</td>
					<td>${p.cities }</td>
				</tr>
			</c:forEach>
		</table>
		<input type="submit" value="show">
	</form>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • 张小帅和刘美美 2021-04-14 15:57
    关注

    首次加载的时候的路径 直接请求到servlet 然后重定向到前端jsp页面  就可以了

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵