Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot ThymeLeaf:如何将从服务器发送的模型属性从ThymeLeaf传递给JS_Spring Boot_Thymeleaf - Fatal编程技术网

Spring boot ThymeLeaf:如何将从服务器发送的模型属性从ThymeLeaf传递给JS

Spring boot ThymeLeaf:如何将从服务器发送的模型属性从ThymeLeaf传递给JS,spring-boot,thymeleaf,Spring Boot,Thymeleaf,我正在通过model属性从服务器向thymeleaf发送一个列表 friendsOnlineModel.setFriendsOnline(defaultFriendRequestService.getFriendsOnline(user) == null ? Collections.EMPTY_LIST : defaultFriendRequestService.getFriendsOnline(user)); chatModel.setChats(defaultChatService

我正在通过model属性从服务器向thymeleaf发送一个列表

friendsOnlineModel.setFriendsOnline(defaultFriendRequestService.getFriendsOnline(user) == null ? Collections.EMPTY_LIST : defaultFriendRequestService.getFriendsOnline(user));

    chatModel.setChats(defaultChatService.getUnreadChats() == null ? Collections.EMPTY_LIST : defaultChatService.getUnreadChats());

    model.addAttribute("friends_online", friendsOnlineModel);

    model.addAttribute("chats", chatModel);

我不想直接在“li”标记中显示它们,而是将其传递给显示它们的js函数。有可能吗?

有几种方法可以做到这一点。您可以使用,并将数据直接添加到页面中。与示例类似:

<script th:inline="javascript">
    var fiendsOnline = [[${friends_online}]];
    var chats = [[${chats}]];
</script>

var fiendsOnline=[${friends_online}]];
var chats=[${chats}]];
然后你可以用javascript处理它们,不管你想要什么


或者,您可以将这些属性添加到另一个控制器方法中,该方法使用
@ResponseBody
注释,而不是将这些属性添加到模型中。然后使用ajax调用该方法,Spring将以JSON的形式返回对象。

有几种方法可以做到这一点。您可以使用,并将数据直接添加到页面中。与示例类似:

<script th:inline="javascript">
    var fiendsOnline = [[${friends_online}]];
    var chats = [[${chats}]];
</script>

var fiendsOnline=[${friends_online}]];
var chats=[${chats}]];
然后你可以用javascript处理它们,不管你想要什么


或者,您可以将这些属性添加到另一个控制器方法中,该方法使用
@ResponseBody
注释,而不是将这些属性添加到模型中。然后使用ajax调用该方法,Spring将以JSON的形式返回您的对象。

非常感谢它的工作,@responsebody方法很慢,因为我希望它在启动时(页面加载时)运行。非常感谢它的工作,@responsebody方法很慢,因为我希望它在启动时(页面加载时)运行。