Controller层的写法
项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对。
跳转到jsp页面:
package com.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import util.AuthConfig; @Controller
public class Operate {
@RequestMapping(value="login.html")
public ModelAndView requestPost(HttpServletRequest request){
AuthConfig config = AuthConfig.getInstance();
String server = config.getConfigParameter("server");
String client_id = config.getConfigParameter("client_id");
String client_secret = config.getConfigParameter("client_secret");
String redirect_uri = config.getConfigParameter("redirect_uri");
String scope = config.getConfigParameter("scope");
String url = server+"AuthServer/oauth2/authorize?client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&response_type=code&scope="+scope;
System.out.println(url);
ModelAndView mav = new ModelAndView("login");
mav.addObject("url", url);
return mav;
}
}
一个是以Json形式返回Map键值对:
package com.bupt.auth.controller; import java.util.HashMap;
import java.util.Map; import com.bupt.oauth2.token.AuthKeyGenerator;
import com.bupt.oauth2.token.DefaultAuthKeyGenerator;
import com.bupt.oauth2.utils.*; import org.springframework.beans.factory.annotation.Autowired; import com.bupt.oauth2.common.OAuth2AccessToken; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.bupt.auth.bussiness.RegisterAppBean;
import com.bupt.auth.bussiness.RegisterBean;
import com.bupt.auth.entity.User;
import com.bupt.auth.exception.MyException;
import com.bupt.auth.service.UserManager;
import com.bupt.oauth2.peve.TokenHandler;
import com.bupt.oauth2.common.util.OAuth2Utils;
import com.bupt.oauth2.common.util.VePeUtils; @RestController
public class RegisteController
{
@Autowired
private TokenHandler tokenHandler;
@Autowired
private UserManager userManager; private AuthKeyGenerator authKeyGenerator = new DefaultAuthKeyGenerator(); @RequestMapping(value="/registeruser.html",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> register(@RequestBody RegisterBean registerbean)
{
Map<String, Object> map = new HashMap<String, Object>();
User user = new User();
try{
if(userManager.findByUsername(registerbean.getUser_name()) != null){
map.put("result", "fail");
map.put("info", "Username has already exist!!");
map.put("failcode", "602");
return map;
}
}catch(MyException e){
map.put("result", "fail");
map.put("info", e.getMessage());
map.put("failcode", e.getFailCode());
return map;
} user.setUsername(registerbean.getUser_name());
user.setPassword(registerbean.getUser_password());
user.setInfo(registerbean.getInfo());
userManager.addUser(user); createUserAccessToken(user);
System.out.println(registerbean.getUser_name()); map.put("result", "success");
return map;
} private void createUserAccessToken(User user)
{
String userName = user.getUsername();
String userId = String.valueOf(user.getId());
Map<String, String> requestParameters = VePeUtils.buildRequestParameters(userId, null);
tokenHandler.accessToken(requestParameters);
//user 中存放authentication_id
//与JdbcTokenStore中storeAccessToken方法中生成authentication_id相对应
user.setAccesstoken(authKeyGenerator.extractKeyByVePeCredentials(user.toString()));
userManager.updateUser(user);
}
}
Controller层的写法的更多相关文章
- @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常==》记录
对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service 层抛出运行时异常,Spring 事物管理器就会进行回滚. 如此一来, ...
- 转:@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
继承 ResponseEntityExceptionHandler 类来实现针对 Rest 接口 的全局异常捕获,并且可以返回自定义格式: 复制代码 1 @Slf4j 2 @ControllerAdv ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
- SpringMVC在Controller层中注入request的坑
记一次为了节省代码没有在方法体中声明HttpServletRequest,而用autowire直接注入所钻的坑 结论 给心急的人. 直接在Controller的成员变量上使用@Autowire声明Ht ...
- spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403
1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...
- DAO层,Service层,Controller层、View层 的分工合作
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- [转]DAO层,Service层,Controller层、View层
来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...
- 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
View层: $('#btnNews').click(function() { if (!confirm('确定要提交吗?')) { return; } var frontViewData = []; ...
随机推荐
- input text框和 checkbox 连带被选中的情况
<html> <head></head> <body> <ul> <li><input type="checkb ...
- cocos2d-x make: *** [clean-box2d_static-armeabi] Error 1
/cygdrive/d/android-ndk-r8e/build/core/build-binary.mk:52: recipe for target `clean-cocos_curl_stati ...
- Linux内核高端内存 转
Linux内核地址映射模型x86 CPU采用了段页式地址映射模型.进程代码中的地址为逻辑地址,经过段页式地址映射后,才真正访问物理内存. 段页式机制如下图. Linux内核地址空间划分 通 ...
- Linux TCP队列相关参数的总结 转
在Linux上做网络应用的性能优化时,一般都会对TCP相关的内核参数进行调节,特别是和缓冲.队列有关的参数.网上搜到的文章会告诉你需要修改哪些参数,但我们经常是知其然而不知其所以然,每次照抄过 ...
- [原创]SSAS-引用维度与多数据源、多数据源视图引发分区错误
背景: 最近有个项目,有32家分公司,集团总部需要取这个32家分公司数据做分析,由于每个分公司的数据都比较庞大,所以最终方案是每个分公司一个DW,在cube搭建过程中将每个公司数据作为一个 ...
- javascript 的位操作符转换推断
var a = "10" | 0; alert(a); alert (typeof a); 结果为10,number. 这就是说这条语句可以将字符串转化为number. 如果: v ...
- AllJoyn Bundled Daemon 使用方式研究
关于AllJoyn不多做介绍,请看官网:www.alljoyn.org/ 0. 问题来源: 应用程序要使用AllJoyn库,就必须启动deamon. 目前有两种方式: 使用standalone形式,单 ...
- 适配iOS9遇到的一些问题_Scheme白名单_ Bitcode及解决办法
升级Xcode7 运行项目发现报错如下: 1.Scheme白名单问题 -canOpenURL: failed for URL: “weixin://app/wxdaae92a9cfe5d54c/” - ...
- solr中通过SFTP访问文件建立索引
需求: 从oracle数据库中根据记录的文件名filename_html(多个文件以逗号隔开),文件路径path,备用文件名bakpath中获取 主机172.21.0.31上对应的html文件内容,并 ...
- PN-Traniger
首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...