1. package com.eduoinfo.finances.bank.web.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.HashMap;  
  8. import java.util.HashSet;  
  9. import java.util.List;  
  10. import java.util.Map;  
  11. import java.util.Set;  
  12. import javax.annotation.Resource;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15. import org.springframework.core.io.ClassPathResource;  
  16. import org.springframework.http.HttpEntity;  
  17. import org.springframework.stereotype.Controller;  
  18. import org.springframework.util.Assert;  
  19. import org.springframework.util.FileCopyUtils;  
  20. import org.springframework.web.bind.annotation.CookieValue;  
  21. import org.springframework.web.bind.annotation.PathVariable;  
  22. import org.springframework.web.bind.annotation.RequestHeader;  
  23. import org.springframework.web.bind.annotation.RequestMapping;  
  24. import org.springframework.web.bind.annotation.RequestParam;  
  25. import org.springframework.web.bind.annotation.ResponseBody;  
  26. import com.eduoinfo.finances.bank.core.entity.JsonResult;  
  27. import com.eduoinfo.finances.bank.web.dao.TestMapper;  
  28. import com.eduoinfo.finances.bank.web.model.Test;  
  29. import com.eduoinfo.finances.bank.web.model.User;  
  30. import com.eduoinfo.finances.bank.web.service.impl.TestServiceImpl;  
  31.   
  32. /** 
  33.  * Spring MVC Controller 编写示例 
  34.  *  
  35.  * @author StarZou 
  36.  * @since 2014年4月26日 上午11:02:20 
  37.  **/  
  38. @Controller  
  39. @RequestMapping(value = "/test")  
  40. public class TestController {  
  41.   
  42.     /** 
  43.      * Service 业务层 注入 
  44.      */  
  45.     @Resource  
  46.     private TestServiceImpl testServiceImpl;  
  47.   
  48.     /** 
  49.      * Dao 注入 
  50.      */  
  51.     @Resource  
  52.     private TestMapper testMapper;  
  53.   
  54.     /** 
  55.      * 通过注解绑定 参数值 示例 
  56.      *  
  57.      * @PathVariable : 从url 中 取得 参数值 
  58.      * @RequestHeader : 从请求头中 取得 参数值 
  59.      * @RequestParam : 从请求参数中 取得 值 
  60.      * @CookieValue : 从 cookie 中取值 
  61.      */  
  62.     @RequestMapping("/**/{id}")  
  63.     @ResponseBody  
  64.     public String testValue(@PathVariable("id") String id, @RequestHeader("User-Agent") String userAgent,  
  65.             @RequestParam(value = "name", defaultValue = "null", required = false) String name, @CookieValue("JSESSIONID") String jsessionid) {  
  66.         return userAgent + "<br>" + jsessionid + "<br>" + id + "<br>" + name;  
  67.     }  
  68.   
  69.     /** 
  70.      * 使用Servlet API对象作为入参 
  71.      *  
  72.      * @param request 
  73.      * @param response 
  74.      */  
  75.     @RequestMapping("/request")  
  76.     public void req(HttpServletRequest request, HttpServletResponse response) {  
  77.         Assert.notNull(request, "请求不能为空");  
  78.         response.setStatus(200);  
  79.     }  
  80.   
  81.     /** 
  82.      * 使用IO对象作为入参 
  83.      *  
  84.      * @param os 
  85.      * @throws IOException 
  86.      */  
  87.     @RequestMapping("/stream/{path}")  
  88.     public void img(OutputStream os, @PathVariable("path") String path) throws IOException {  
  89.         ClassPathResource res = new ClassPathResource("/spring-mvc.xml");  
  90.         FileCopyUtils.copy(res.getInputStream(), os);  
  91.   
  92.     }  
  93.   
  94.     /** 
  95.      * 表单提交 示例,bean的属性 自动 装配 <br> 
  96.      * 指定方法类型:method = RequestMethod.POST <br> 
  97.      * http://localhost/test/rest/test/body?name=starzou&password=123 
  98.      *  
  99.      * @param user 
  100.      * @return 
  101.      */  
  102.     @RequestMapping(value = "/body")  
  103.     @ResponseBody  
  104.     public Object body(User user) {  
  105.         return user;  
  106.     }  
  107.   
  108.     /** 
  109.      * httpEntity.getHeaders() 得到所有请求头 
  110.      *  
  111.      * @param httpEntity 
  112.      * @return 
  113.      */  
  114.     @RequestMapping("/http")  
  115.     @ResponseBody  
  116.     public Object http(HttpEntity<String> httpEntity) {  
  117.         return httpEntity.getHeaders().entrySet();  
  118.     }  
  119.   
  120.     /** 
  121.      * 调用 业务层 方法示例 
  122.      *  
  123.      * @return 
  124.      */  
  125.     @RequestMapping("/date")  
  126.     @ResponseBody  
  127.     public String date() {  
  128.         return testServiceImpl.date();  
  129.     }  
  130.   
  131.     /** 
  132.      * Spring MVC将匹配方法参数名URI模板变量名称 
  133.      *  
  134.      * @param x 
  135.      * @param y 
  136.      * @return 
  137.      */  
  138.     @RequestMapping("/var/{x}/{y}")  
  139.     @ResponseBody  
  140.     public String var(@PathVariable String x, @PathVariable String y) {  
  141.         return x + "<br>" + y;  
  142.     }  
  143.   
  144.     /** 
  145.      * 测试 mybaits generator生成的 dao model,进行 数据库操作 
  146.      *  
  147.      * @return 
  148.      */  
  149.     @RequestMapping("/tt/add")  
  150.     @ResponseBody  
  151.     public Object testAdd() {  
  152.         com.eduoinfo.finances.bank.web.model.Test test = new com.eduoinfo.finances.bank.web.model.Test();  
  153.         final long currentTimeMillis = System.currentTimeMillis();  
  154.         test.setTname("tname : " + currentTimeMillis);  
  155.         test.setTdate(new Date());  
  156.         testMapper.insert(test);  
  157.         return test;  
  158.     }  
  159.   
  160.     /** 
  161.      * 数据库 查询 
  162.      *  
  163.      * @return 
  164.      */  
  165.     @RequestMapping("/tt/select")  
  166.     @ResponseBody  
  167.     public Object testSelect() {  
  168.         final List<Test> list = testMapper.selectByExample(null);  
  169.         return list;  
  170.     }  
  171.   
  172.     /** 
  173.      * 测试 spring mvc 返回 json , 封装 Json 格式数据, 减少 类型转换 
  174.      *  
  175.      * @return 
  176.      */  
  177.     @RequestMapping("/json")  
  178.     @ResponseBody  
  179.     public JsonResult<Object[]> returnJson() {  
  180.         // 实际情况 下 String,可能是一个 自定义的Java 类,比如 User , 通常是在 数据库查询  
  181.         List<String> data = new ArrayList<>();  
  182.         Set<String> data2 = new HashSet<>();  
  183.         Map<String, String> data3 = new HashMap<>();  
  184.   
  185.         int i = 0;  
  186.         while (i < 10) {  
  187.             String value = "data-" + (++i);  
  188.             data.add(value);  
  189.             data2.add(value);  
  190.             data3.put(value, value);  
  191.         }  
  192.   
  193.         // 组装 查询的 结果 , 添加消息 和 是否成功的标识  
  194.         JsonResult<List<String>> jsonResult = new JsonResult<>(data, "This is a message.", true);  
  195.         JsonResult<Set<String>> jsonResult2 = new JsonResult<>(data2, "This is a message.", true);  
  196.         JsonResult<Map<String, String>> jsonResult3 = new JsonResult<>(data3, "This is a message.", true);  
  197.   
  198.         // 复杂一点的 封装  
  199.         Object[] objs = { jsonResult, jsonResult2, jsonResult3 };  
  200.         JsonResult<Object[]> jsonObj = new JsonResult<Object[]>(objs);  
  201.         return jsonObj;  
  202.     }  
  203. }  
posted on 2014-11-22 20:31  小光zfg  阅读(203)  评论(0)    收藏  举报