Controller层的写法

Posted godlei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Controller层的写法相关的知识,希望对你有一定的参考价值。

项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对。

跳转到jsp页面:

 1 package com.controller;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.commons.httpclient.HttpClient;
 8 import org.apache.commons.httpclient.HttpException;
 9 import org.apache.commons.httpclient.methods.PostMethod;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.servlet.ModelAndView;
13 
14 import util.AuthConfig;
15 
16 @Controller
17 public class Operate {
18     @RequestMapping(value="login.html")
19     public ModelAndView requestPost(HttpServletRequest request){
20         AuthConfig config = AuthConfig.getInstance();
21         String server = config.getConfigParameter("server");
22         String client_id = config.getConfigParameter("client_id");
23         String client_secret = config.getConfigParameter("client_secret");
24         String redirect_uri = config.getConfigParameter("redirect_uri");
25         String scope = config.getConfigParameter("scope");
26         String url = server+"AuthServer/oauth2/authorize?client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&response_type=code&scope="+scope;
27         System.out.println(url);
28         ModelAndView mav = new ModelAndView("login");
29         mav.addObject("url", url);
30         return mav;
31     }
32 }

一个是以Json形式返回Map键值对:

 1 package com.bupt.auth.controller;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 import com.bupt.oauth2.token.AuthKeyGenerator;
 7 import com.bupt.oauth2.token.DefaultAuthKeyGenerator;
 8 import com.bupt.oauth2.utils.*;
 9 
10 import org.springframework.beans.factory.annotation.Autowired;
11 
12 import com.bupt.oauth2.common.OAuth2AccessToken;
13 
14 import org.springframework.web.bind.annotation.RequestBody;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.ResponseBody;
18 import org.springframework.web.bind.annotation.RestController;
19 
20 import com.bupt.auth.bussiness.RegisterAppBean;
21 import com.bupt.auth.bussiness.RegisterBean;
22 import com.bupt.auth.entity.User;
23 import com.bupt.auth.exception.MyException;
24 import com.bupt.auth.service.UserManager;
25 import com.bupt.oauth2.peve.TokenHandler;
26 import com.bupt.oauth2.common.util.OAuth2Utils;
27 import com.bupt.oauth2.common.util.VePeUtils;
28 
29 @RestController
30 public class RegisteController 
31 {
32     @Autowired
33     private TokenHandler tokenHandler;
34     @Autowired
35     private UserManager userManager;
36     
37     private AuthKeyGenerator authKeyGenerator = new DefaultAuthKeyGenerator();
38     
39     @RequestMapping(value="/registeruser.html",method=RequestMethod.POST)
40     @ResponseBody
41     public Map<String, Object> register(@RequestBody RegisterBean registerbean)
42     {
43         Map<String, Object> map = new HashMap<String, Object>();
44         User user = new User();
45         try{
46             if(userManager.findByUsername(registerbean.getUser_name()) != null){
47                 map.put("result", "fail");
48                 map.put("info", "Username has already exist!!");
49                 map.put("failcode", "602");
50                 return map;
51             }
52         }catch(MyException e){
53             map.put("result", "fail");
54             map.put("info", e.getMessage());
55             map.put("failcode", e.getFailCode());
56             return map;
57         }
58         
59         user.setUsername(registerbean.getUser_name());
60         user.setPassword(registerbean.getUser_password());
61         user.setInfo(registerbean.getInfo());
62         userManager.addUser(user);
63         
64         createUserAccessToken(user);
65         System.out.println(registerbean.getUser_name());
66         
67         map.put("result", "success");
68         return map;
69     }
70     
71     private void createUserAccessToken(User user)
72     {
73         String userName = user.getUsername();
74         String userId = String.valueOf(user.getId());
75         Map<String, String> requestParameters =  VePeUtils.buildRequestParameters(userId, null);
76         tokenHandler.accessToken(requestParameters);
77         //user 中存放authentication_id
78         //与JdbcTokenStore中storeAccessToken方法中生成authentication_id相对应
79         user.setAccesstoken(authKeyGenerator.extractKeyByVePeCredentials(user.toString()));
80         userManager.updateUser(user);
81     }
82 }

 

以上是关于Controller层的写法的主要内容,如果未能解决你的问题,请参考以下文章

高并发下Service层的写法

controller 有两种写法,讨论一下两种写法的区别:

Service层和Controller层的开发

支持jsonP的Controller写法

mask遮罩层的华丽写法

简单的Dao层的模板写法