gpt4 book ai didi

java - Spring webapp 显示数据库中的动态内容

转载 作者:行者123 更新时间:2023-12-02 10:53:17 25 4
gpt4 key购买 nike

我有一个用 Spring Boot 制作的 REST API。我可以使用 HTTP 请求将实体(人员)添加到数据库中。我希望有一个网页动态显示这些实体:每当数据库中存储新人员时,页面都会将其显示在列表顶部。无需刷新页面。

有哪些方法可以实现这一目标? JSP 就足够了吗?我需要 JavaScript 吗?

越简单越好。

最佳答案

What are the ways to achieve this? Would JSP be enough? Do I need javascript?

是的,你需要 JavaScript。但您也可以将其嵌入到 JSP 中。

这是一个已解决的问题,如果您搜索“websocket spring”,您会在网络上找到大量示例。示例https://spring.io/guides/gs/messaging-stomp-websocket/

如果您没有陷入 JVM 领域和 RDBMS,我鼓励您看看 firebase-angular stack(3 路绑定(bind))

进一步阅读

阅读此内容 https://stackoverflow.com/a/12855533/6785908

无耻的复制,因为仅发布链接是不可取的,因为链接将来可能会损坏

In the examples below the client is the browser and the server is the webserver hosting the website.

Before you can understand these technologies, you have to understand classic HTTP web traffic first.

Regular HTTP:

  1. A client requests a webpage from a server.
  2. The server calculates the response
  3. The server sends the response to the client.

HTTP

Ajax Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at regular intervals (e.g. 0.5 seconds).
  3. The server calculates each response and sends it back, just like normal HTTP traffic.

Ajax Polling

Ajax Long-Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
  3. The server does not immediately respond with the requested information but waits until there's new information available.
  4. When there's new information available, the server responds with the new information.
  5. The client receives the new information and immediately sends another request to the server, re-starting the process.

Ajax Long-Polling

HTML5 Server Sent Events (SSE) / EventSource:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
  3. The server sends an event to the client when there's new information available.

    • Real-time traffic from server to client, mostly that's what you'll need
    • You'll want to use a server that has an event loop
    • Not possible to connect with a server from another domain
    • If you want to read more, I found these very useful: (article), (article), (article), (tutorial).

HTML5 SSE

HTML5 Websockets:

  1. A client requests a webpage from a server using regular http (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
  3. The server and the client can now send each other messages when new data (on either side) is available.

    • Real-time traffic from the server to the client and from the client to the server
    • You'll want to use a server that has an event loop
    • With WebSockets it is possible to connect with a server from another domain.
    • It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to implement the client side, which is very easy!
    • If you want to read more, I found these very useful: (article), (article) (tutorial).

HTML5 WebSockets

Comet:

Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Read more on wikipedia or this article.

关于java - Spring webapp 显示数据库中的动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com