简体   繁体   English

在Spring中创建活动供稿的正确方法

[英]Proper way to create activity feed in Spring

In a section of my page, I need to have ActivityFeed which shows latest entered data from a user. 在页面的一部分中,我需要具有ActivityFeed,该ActivityFeed显示来自用户的最新输入数据。

So there's the User model.. And the Survey model. 因此,这里有用户模型和调查模型。

The user gives some input, and then they show up on the Survey table. 用户提供一些输入,然后它们出现在“调查”表中。 The Survey model has a field with a timestamp Survey模型的字段带有时间戳

@CreationTimestamp
@Column(name = "submitedTime",nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date submittedDate;

So what I did is on the SurveyRepository called a method : 所以我所做的是在SurveyRepository上称为方法:

List<Survey> findTop10ByOrderBySubmittedDateDesc();

And this returns me the 10 latest submitted surveys. 这将返回我最近提交的10份调查。 Then I create an endpoint for this : 然后我为此创建一个端点:

  @RequestMapping(value="getActivityFeed",method=RequestMethod.GET)
public @ResponseBody List<Survey> getSurveys(){
    return surveyService.findTop10ByOrderBySubmittedDateDes();
}

Then with an Ajax Call from my JS I call the Endpoint, and pull out information. 然后,通过我的JS中的Ajax调用,我调用了端点,并提取了信息。

Is this the proper way to create an Activity feed? 这是创建活动供稿的正确方法吗? Or is there a way that after every submitted value it, the Activity Feed gets updated ? 还是有办法在每个提交的值之后更新活动Feed?

I am using Spring-Boot and Thymeleaf too. 我也在使用Spring-Boot和Thymeleaf。

THanks 谢谢

I suppose you are using a setInterval to fetch the recent activities via Ajax. 我想您正在使用setInterval通过Ajax获取最近的活动。 That is ok, but a better way of doing that is using websocket(avoiding users polling your server consuming precious resources): 可以,但是更好的方法是使用websocket(避免用户轮询消耗宝贵资源的服务器):

WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. WebSockets是一项先进的技术,可以打开用户浏览器和服务器之间的交互式通信会话。

(MDN Webdocs - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API ) (MDN Webdocs- https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

It is very lightweight for you purpose, and I believe it is the best choice for updating user's view based on real backend events. 对于您而言,它非常轻巧,我相信这是基于真实的后端事件更新用户视图的最佳选择。

Take a better look at Mozilla's reference and check these other references as well: 更好地了解Mozilla的参考,并检查以下其他参考:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM