SpringSocial提供了了以下三个服务,我们要做的仅仅是调用它们的服务,但是SpringSocial仅仅只提供了数据,没有提供视图

⒈拿到所有社交网站与业务系统的绑定信息

  SpringSocial已经提供了相应的数据,但并没有提供视图,相关的代码写在ConnectController中,核心代码如下:

     @RequestMapping(
method = {RequestMethod.GET}
)
public String connectionStatus(NativeWebRequest request, Model model) {
this.setNoCache(request);
this.processFlash(request, model);
Map<String, List<Connection<?>>> connections = this.connectionRepository.findAllConnections();
model.addAttribute("providerIds", this.connectionFactoryLocator.registeredProviderIds());
model.addAttribute("connectionMap", connections);
return this.connectView();
}

  这个方法的返回值是一个View,路径是connect/status,我们只需要实现这个视图就可以了。

 package cn.coreqi.social;

 import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @Component("connect/status")
public class CoreqiConnectionStatusView extends AbstractView { @Autowired
private ObjectMapper objectMapper;
@Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
Map<String, List<Connection<?>>> connections = (Map<String, List<Connection<?>>>) map.get("connectionMap");
Map<String,Boolean> result = new HashMap<>();
for(String key:connections.keySet()){
result.put(key, CollectionUtils.isNotEmpty(connections.get(key)));
}
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
}
}

⒉业务系统与社交网站绑定

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定</title>
</head>
<body>
<h2>标准绑定页面</h2>
<!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
<form action="/connect/weixin" method="post">
<button type="submit">绑定微信</button>
</form>
</body>
</html>

绑定成功后跳转到connect/{providerId}Connected这个视图,上面的例子将会跳转到connect/weixinConnected这个视图上。

 package cn.coreqi.social;

 import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map; @Component("connect/weixinConnect,connect/weixinConnected") //如果当前绑定成功的视图需要重用,请注释@Component注解
//在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
public class CoreqiConnectionView extends AbstractView { @Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("text/html;charset=UTF-8");
if(map.get("connection") == null){
httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
}else{
httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
}
}
}

⒊业务系统与社交网站解绑

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定</title>
</head>
<body>
<h2>标准绑定页面</h2>
<!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
<!--解除绑定只需将post请求改为delete请求,当然,form表单是无法发送delete请求的,我只是告诉你-->
<form action="/connect/weixin" method="delete">
<button type="submit">绑定微信</button>
</form>
</body>
</html>

解绑成功后跳转到connect/{providerId}Connect这个视图(相较于绑定,视图名去掉了ed而已),上面的例子将会跳转到connect/weixinConnect这个视图上。

 package cn.coreqi.social;

 import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map; @Component("connect/weixinConnect,connect/weixinConnected") //如果当前绑定成功的视图需要重用,请注释@Component注解
//在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
public class CoreqiConnectionView extends AbstractView { @Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("text/html;charset=UTF-8");
if(map.get("connection") == null){
httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
}else{
httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
}
}
}

SpringSocial业务系统与社交网站的绑定与解绑的更多相关文章

  1. Service的启动与停止、绑定与解绑

    ---恢复内容开始--- Service的意义就在于当软件停止之后还可以在背景中进行运行,换句话也就是说,比如一个音乐播放器,当我们退出音乐播放器的时候,还是希望它在背景中运行,也就是一直播放着音乐, ...

  2. React事件绑定与解绑

    React中事件分类 React中事件绑定分为两种: 1.直接添加在React元素上的事件,这是React在基于Virtual DOM的基础上实现的符合w3c规范的合成事件(SyntheticEven ...

  3. 兼容8事件绑定与解绑addEventListener、removeEventListener和ie的attachEvent、detachEvent

    兼容8事件绑定与解绑addEventListener.removeEventListener和ie的attachEvent.detachEvent   ;(function(){ // 事件绑定 bi ...

  4. jQuery事件绑定、解绑、命名空间

    jQuery事件绑定.解绑.命名空间 <%@ page language="java" import="java.util.*" pageEncoding ...

  5. 使用JavaScript动态的绑定、解绑 a 标签的onclick事件,防止重复点击

    页面上的 a 标签如下: <a class="more" style="cursor: pointer;" id="commentMore&qu ...

  6. jquery中的DOM事件绑定与解绑

    在jquery事件中有时候有的事件只需要在绑定后有效触发一次,当通过e.target判断触发条件有效触发后解除绑定事件,来避免多次无效触发和与未知情况造成冲突. 这时候就要用到了jquery中的事件绑 ...

  7. jquery中事件重复绑定以及解绑问题

    一般的情况下,对于这种情况,我们常规的思路是,先解绑,再绑定,如下: $(selector).unbind('click').bind('click',function(){....}); 当这样会有 ...

  8. jQuery事件绑定,解绑,触发

    事件绑定 1.bind(type,[data],fn) --type: 含有一个或多个事件类型的字符串,由空格分隔多个事件.比如"click"或"submit" ...

  9. jQuery 学习笔记(5)(事件绑定与解绑、事件冒泡与事件默认行为、事件的自动触发、自定义事件、事件命名空间、事件委托、移入移出事件)

    1.事件绑定: .eventName(fn) //编码效率略高,但部分事件jQuery没有实现 .on(eventName, fn) //编码效率略低,所有事件均可以添加 注意点:可以同时添加多个相同 ...

随机推荐

  1. maven_eclipse配置maven

    1.eclipse配置3.3.9版本的maven 2.修改仓库位置

  2. Mybatis笔记二:接口式编程

    目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...

  3. golang interface

    接口定义 Interface类型可以定义一组方法,但是这些不需要实现.并且interface不能 包含任何变量. type Interface interface { test1(a, b int) ...

  4. HDU - 4027 Can you answer these queries?(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...

  5. Openresty 学习笔记(四)lualocks包管理器安装使用

    Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖.安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比 ...

  6. JVM垃圾回收机制与内存回收

    暂时转于:https://blog.csdn.net/qq_27035123/article/details/72857739 垃圾回收机制 GC是垃圾回收机制,java中将内存管理交给垃圾回收机制, ...

  7. 前端面试题整理—HTML/CSS篇

    1.简述一下你对HTML语义化的理解 1)用正确的标签做正确的事情 2)html语义化让页面的内容结构化,结构更清晰,便于对浏览器.搜索引擎解析 3)即使在没有样式CSS情况下也以一种文档格式显示,并 ...

  8. 自学python 6.

    内容:id() is == 编码 解码1.好声音选秀比赛评委在打分的时候可以进行输入. 假设有10个评委.让10个评委进行打分, 要求, 分数必须大于5分, 小于10分.count = 1while ...

  9. 【二】Spring Cloud 入门

    官网 版本号: SpringCloud中文网:https://springcloud.cc SpringCloud中文社区:http://springcloud.cn 以下代码就是Maven父子工程, ...

  10. JQuery Rest客户端库

    JQuery Rest https://github.com/jpillora/jquery.rest/ Summary A jQuery plugin for easy consumption of ...