簡體   English   中英

使用Spring將路徑變量傳遞給控制器

[英]Passing A Path Variable To A Controller With Spring

我正在使用spring創建一個與facebook鏈接的應用程序。 啟動應用程序后,它會將您重定向到登錄頁面。 單擊“登錄”按鈕后,它會將您重定向到主頁。 我想做的是,當單擊“登錄”按鈕時,它會將您重定向到主頁,並在末尾添加一個唯一的數字。 每當我啟動我的應用程序時,我都會收到HTTP Status 404錯誤。 我不知道是什么問題。 編輯我正在使用Spring Social Quickstart,我對程序所做的唯一修改是以下內容

登錄頁面

    <%@ page session="false" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
        <head>
            <title>Sign In</title>
        </head>
        <body>
<c:set var="rand"><%= java.lang.Math.round(java.lang.Math.random() * 2) %></c:set>
            <form action="<c:url value="/signin/facebook/${rand}" />" method="POST">
                <button type="submit">Sign in with Facebook</button>
                <input type="hidden" name="scope" value="email,publish_stream,offline_access" />
            </form>
        </body>
    </html>

主頁

<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
        <title>Home</title>
    </head>
    <body>
    <ul>
        <li><a href="<c:url value="/signout" />">Sign Out</a></li>
    </ul>
        <p>${number}</p>
    <h3>Your Facebook Friends</h3>
    <ul>
    <c:forEach items="${friends}" var="friend">
        <li><img src="http://graph.facebook.com/<c:out value="${friend.id}"/>/picture" align="middle"/><c:out value="${friend.name}"/></li>
    </c:forEach>
    </ul>   
    </body>
</html>

家庭控制器

@RequestMapping(value = "/{rand}", method = RequestMethod.GET)
public String home(@PathVariable("rand") String rand, Model model) throws IOException {

           List<Reference> friends = facebook.friendOperations().getFriends();
           FacebookProfile profile = facebook.userOperations().getUserProfile();
           String userID = facebook.userOperations().getUserProfile().getId();
           model.addAttribute("friends", friends);
           String accessToken = connectionRepository.getPrimaryConnection(Facebook.class).createData().getAccessToken();
            System.out.println(accessToken);
        return "home";
    }

您的應用程序的基本URL是什么,您的控制器也有根URL嗎? <c:url value="/signin/facebook/${rand}" />代碼行將產生類似於http://localhost:8080/yourAppname/signin/facebook/12342354665

根據配置和其他注釋,您應將@RequestMapping(value = "/{rand}", method = RequestMethod.GET)更改為@RequestMapping(value = "/signin/facebook/{rand}", method = RequestMethod.GET) 您當前的映射(取決於控制器注釋)僅提供根級URL。

您已啟用日志記錄嗎? 您應該能夠在日志中看到失敗的映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM