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

在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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装