如何在Spring Boot

时间:2017-05-10 21:39:11

标签: java spring twitter spring-boot thymeleaf

我正在使用Tymeleaf的Spring Boot并且对它很新。我正在使用Twitter API制作应用程序。我有一个使用th:each显示的推文列表。当用户点击推文的转发按钮时,该特定推文的id应作为转推方法的参数发送给控制器,以便在那里进行处理。用户不应该知道前端的id。

截至目前,我唯一的解决方案是将id作为url包含在'a'标记中,并将其作为路径变量在控制器中使用@PathVariable并返回包含该列表的索引页推文。这工作正常但我不喜欢这种方法,因为用户不应该离开页面,我可能以后更喜欢使用模态。基本上我想知道一种将id发送到控制器而不用HTML显示的方法。

的index.html

 <div class="result" th:each="result : ${results}">
            <h3><a th:href="${result.fromUser}" th:text="${result.user.name}"></a></h3>

            <p th:text="'@' + ${result.fromUser}"></p>
            <span th:text="${result.text}"></span>
            <div id="retweet">
                    <a th:href="'user/' +  ${result.id}">Retwt</a>

            </div>
        </div>

Maincontroller.java

@RequestMapping(value = "")
public String getUserProfile(Model model) {
    TwitterProfile userInfo = twitterService.getUserProfile();
    List<Tweet> homeTimeline = twitterService.getUserTimeline();
    model.addAttribute("profileImg", userInfo.getProfileImageUrl());
    model.addAttribute("results", homeTimeline);
    return "pages/index";
}

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String retweet(@PathVariable long id) {
    twitterService.retweet(id);
    return "/";
}

1 个答案:

答案 0 :(得分:1)

这可以通过改变你的控制器方法来实现,并使用jQuery ajax http GET访问它。

更改控制器以接受ajax请求,

    @ResponseBody    
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String retweet(@RequestBody Long id) {
        twitterService.retweet(id);
        return "/";
    }

更改您的HTML

 <div class="result" th:each="result : ${results}">
            <h3><a th:href="${result.fromUser}" th:text="${result.user.name}"></a></h3>

            <p th:text="'@' + ${result.fromUser}"></p>
            <span th:text="${result.text}"></span>
            <div id="retweet">
                    <a href="JavaScript:Void(0);" th:onclick="'doRetweet(\'' + ${result.id} + '\');'" >Retwt</a>

            </div>
        </div>

JQuery部分,

                     function doRetweet(userId){
                        var data = JSON.stringify(userId)
                        if (data) {
                            $.ajax({
                                url : '/user',
                                headers : {
                                    'Content-Type' : 'application/json'
                                },
                                method : 'GET',
                                dataType : 'json',
                                data : data,
                                success : function(data) {
                                    if (data.status == 200) {
                                        // Handle success response
                                    } 
                                },

                                error : function(xhr, status, error) {
                                    // Handle errors here
                                }
                            });
                        }
                    }