简体   繁体   English

如何使用jsp视图检查某个用户是否已创建数据库项

[英]How to check if certain user has made database items using jsp view

I have a database table called bookings, and what i'm trying to do but couldn't find so far, is to check with if-else, if there are bookings made by certain user, and display them in a table. 我有一个名为bookings的数据库表,我正在尝试但到目前为止找不到的是用if-else检查,如果有某些用户预订,并将它们显示在表中。

在此输入图像描述

So i'm looking for something like this: 所以我正在寻找这样的东西:

(semi-pseudo code below) (下面的半伪代码)

<c:when test="${bookings.where(booking.userId == currentUserId) > 0}">
   display table with those items...
</c:when>

so this would get me all the table items made by that user 所以这将得到该用户制作的所有表项

How can i test this in a jsp view page? 我如何在jsp视图页面中测试它?

Thanks in advance. 提前致谢。

PS: displaying the items is not a problem, i can at least do that on my own :) PS:显示项目不是问题,我至少可以自己做:)

EDIT backend part : 编辑后端部分

@GetMapping("/bookings")
    public String getAllBookings(Model model) {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetail = (UserDetails) auth.getPrincipal();

        User user = userService.findByUsername(userDetail.getUsername());
        int currentUserId = user.getId();

        List<Booking> bookings = bookingService.getAllBookings();
        model.addAttribute("bookings", bookings);

        model.addAttribute("currentUserId", currentUserId);
        model.addAttribute("currentUser", getPrincipal());

        return "booking-list";
    }

First of all you probably should do this on backend and just pass filtered list, but for the jsp approach you can use forEach . 首先,您可能应该在后端执行此操作并只传递已过滤的列表,但对于jsp方法,您可以使用forEach

Example: 例:

<c:forEach items="${bookings}" var="booking">
    <c:if test="${booking.userId == currentUserId}">
       ...
    </c:if>
</c:forEach>

Edit: question updated 编辑:问题已更新

To filter bookings in the backend you could use Stream API : 要过滤后端中的预订,您可以使用Stream API

List<Booking> bookings = bookingService.getAllBookings().stream()
        .filter(b -> b.getUserId() == currentUserId)
        .collect(Collectors.toList());
model.addAttribute("bookings", bookings);

Edit 2.0: 编辑2.0:

Actually you shouldn't even filter them in java. 实际上你甚至不应该在java中过滤它们。 Since you take the data from database, you should implement getBookingsForUserId() and use WHERE clause to filter them. 由于从数据库中获取数据,因此应实现getBookingsForUserId()并使用WHERE子句对其进行过滤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查我的Firebase数据库是否具有特定用户的数据? (Android / Java) - How can I check if my Firebase database has data for a certain user? (Android/Java) 使用 JDBC 和 JSP 进行登录身份验证:如何检查用户输入的用户名和密码是否存在于数据库中? - Login Authentication using JDBC and JSP : How to check if the username and Password entered by the user exists in the Database? 如何检查是否已在数据库中插入具有某些值的特定行 - How to check if a specific row with certain values has been inserted in the Database 如何检查某个用户是否具有读者对文档的访问权限 - How to check if a certain user has reader-access to a document 如何检查JSP是否连接到数据库? - How to check if JSP is connected to a database? JSP:如何检查用户是否已登录? - JSP: How to check if a user is logged in? 如何在不使用 jsp 提交表单的情况下检查数据库中是否存在用户名? - How to check if username exists in database or not without submitting the form using jsp? 如果用户登录(如果使用JSTL <if>语句),如何检查JSP,并显示用户名? - How to check in JSP if user is logged (using JSTL <if> statement), and display username? 如何使用jsp从数据库获取JSON格式的视图数据 - how to get view data in JSON format from database using jsp 如何通过 JSP 使用用户输入数据创建和查看会话密钥 - How to create and view session keys using user input data with JSP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM