求助,

游泳的考拉 2023-08-16 23:13:46

测试类是可以获取到数据库的数据的,但是jsp页面就是不显示,不知道是什么原因。。  servlet:

package com.xxxx.controller;

import com.xxxx.entity.User;
import com.xxxx.service.UserService;
import com.xxxx.service.UserServiceImpl;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/userListServlet")
public class UserController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        调用UserService完成查询
            UserService service = new UserServiceImpl();
            List<User> users = service.getUserList();
//            HttpSession session = request.getSession();
//            session.setAttribute("users",users);
//            request.getRequestDispatcher("/list.jsp").forward(request,response);
//        将list存入request域
        request.setAttribute("users",users);
//        转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<meta charset="UTF-8">
<head>
    <title>Title</title>
    <link href="css/bootstrap-5.1.3-dist-css/css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery-3.4.1.js"></script>
    <script src="js/bootstrap-5.1.3-dist-js/js/bootstrap.min.js"></script>
    <style type="text/css">
        td,th{
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h3 style="text-align: center">用户信息表</h3>
        <table border="1" class="table table-bordered table-hover">
            <tr class="success">
                <th>编号</th>
                <th>用户名</th>
                <th>性别</th>
                <th>学号</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${users}" var="User" varStatus="s">
<%--                <%--%>
<%--                    UserController userController = new UserController();--%>
<%--                    userController.doPost(request,response);--%>
<%--                %>--%>
                <tr>
<%--                    <th>${s.count}</th>--%>
                    <th>${User.username}</th>
                    <th>${User.gender}</th>
                    <th>${User.student_id}</th>
                    <th>${User.mail}</th>
                    <th><a class="btn btn-default btn-sm" href="">修改</a>&nbsp;
                    <a class="btn btn-default btn-sm" href="">删除</a> </th>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

 

...全文
5517 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
游泳的考拉 2023-08-17
  • 打赏
  • 举报
回复

我明白了,访问的时候不需要跳转到list.jsp页面,直接跳转userListServlet就行了

游泳的考拉 2023-08-17
  • 打赏
  • 举报
回复

我直接访问/userListServlet可以显示数据。但是list.jsp却显示不了,这是为什么?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧