Http POST方法和Spring MVC RedirectView

时间:2013-09-29 19:28:26

标签: java spring-mvc spring-social

使用Spring Social和Spring MVC时,我尝试在连接到社交提供程序时使用RedirectView,并且必须遗漏某些内容,并且无法成功连接到LinkedIn。

以下是2种控制器方法:第一种方法按预期运行:根据用户的服务提供商选择,它通过sp.isconnected()呼叫检测到它未连接到已连接,何时未重定向到由第二种方法控制的视图,用于启动链接连接。

第二种方法设置适当的权限参数并重定向到适当的spring社交连接URL(URIs.SPRINGLISIGNIN =“/ connect / linkedin”),但Spring Social ConnectController在这个阶段会期望一个POST方法而我无法管理弄清楚如何通过RedirectView传递它。我期待第一个控制器方法的初始POST方法将被执行到第二个,但事实并非如此。在lisgnin方法的调试模式下,request.getMethod()显示GET值。

   @RequestMapping(value = Uris.SPCHOICE, method = RequestMethod.POST)
public RedirectView Spchoice (@RequestParam("sp") String sp, Model model) {

    ServiceProviders spasenum = ServiceProviders.valueOf(sp);
    RedirectView toReturn;
    SPConnectionRetriever spResolver=null;
    switch (spasenum) {
    case FACEBOOK :
        spResolver = FBConnectionRetriever;   
        break;
    case LINKEDIN :
        spResolver = LIConnectionRetriever;     
        break;
    }
    SecurityContext.setCurrentSpResolver(spResolver);
    if (spResolver.isconnected())
        toReturn  = new RedirectView(Uris.MAIN, true);
    else
        toReturn = new RedirectView(spResolver.getConnectUrl(), true);

    return toReturn;
}

这是第二种方法:

@RequestMapping(value = Uris.SIGNINLI)
public RedirectView liSignin(HttpServletRequest request, HttpServletResponse response) {
    request.setAttribute("scope", "r_fullprofile,r_network");
    return new RedirectView(Uris.SPRINGLISIGNIN, true);

1 个答案:

答案 0 :(得分:0)

根据Roman C评论,没有回到浏览器就无法实现我想做的事情,以便它可以发出POST请求。 所以我通过重新构建代码并专注于JSP来实现我想要做的事情。

    <h3>
    Showing available Service Providers :
    <c:out value="${serviceProvider}" />
    </h3>
    <ul>
    <c:forEach items="${serviceProviders}" var="sp">
        <li><c:out value="${sp.name}" /> <c:if test="${sp.connected}"> : Connected with Permissions : <c:out
                    value="${sp.permissions}" />
                <form action="<c:url value="<%=Uris.DISCONNECT%>" />" method="POST">
                    <button type="submit">Disconnect</button>
                    <input type="hidden" name="sp" value="${sp.name}" />
                </form>
            </c:if> 
            <c:if test="${!sp.connected}"> : Disconnected  
            <form action="<c:url value="${sp.URL }" />" method="POST">
                    <button type="submit">Connect</button>
                    <input type="hidden" name="scope" value="${sp.permissions}" />
                </form></c:if></li>
    </c:forEach>
</ul>

然后,控制器根据其连接状态负责填充服务提供商信息。如果没有与服务提供者的连接,则第二个<c:if>子句将构建一个连接按钮,负责使用正确的范围/权限执行post方法。

这是控制器代码(URIs.WORK指向上面显示的jsp):

   @RequestMapping(value = Uris.MAIN)
    public String home(Model model) {
    List <SPInfo> SPStatusList = new ArrayList<SPInfo>();
    for (ServiceProviders sp : ServiceProviders.values()) {
        SPConnectionRetriever spAccess = SPBank.getSPConnection(sp);
        SPStatusList.add(new SPInfo(sp.toString(), spAccess.isconnected(), spAccess.getPermissions(),spAccess.getConnectUrl()));
    }

    model.addAttribute("nom", SecurityContext.getCurrentUser().getId());
       model.addAttribute("serviceProviders", SPStatusList);

    return Uris.WORK;
}

SPInfo类如下所示:

   public class SPInfo {
    private String name;
    private boolean connected;
    private String permissions;
    private String URL;

    public SPInfo(String name, boolean isConnected, String permissions, String URL) {
        super();
        this.name = name;
        this.connected = isConnected;
        this.permissions = permissions;
        this.URL = URL;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean isConnected() {
        return connected;
    }
    public void setConnected(boolean isConnected) {
        this.connected = isConnected;
    }
    public String getPermissions() {
        return permissions;
    }
   public void setPermissions(String permissions) {
        this.permissions = permissions;
    }
    public String getURL() {
        return URL;
    }
    public void setURL(String uRL) {
        URL = uRL;
    }   
}