package com.sklll.web.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sklll.core.entity.JsonResult;
import com.sklll.web.dao.TestMapper;
import com.sklll.web.model.Test;
import com.sklll.web.model.User;
import com.sklll.web.service.impl.TestServiceImpl;
/**
* Spring MVC Controller 编写示例
*
* @author shenkunlin
* @since 2016年8月28日 上午9:12:56
**/
@Controller
@RequestMapping(value = "/test")
public class TestController {
/**
* Service 业务层 注入
*/
@Resource
private TestServiceImpl testServiceImpl;
/**
* Dao 注入
*/
@Resource
private TestMapper testMapper;
/**
* 通过注解绑定 参数值 示例
*
* @PathVariable : 从url 中 取得 参数值
* @RequestHeader : 从请求头中 取得 参数值
* @RequestParam : 从请求参数中 取得 值
* @CookieValue : 从 cookie 中取值
*/
@RequestMapping("/**/{id}")
@ResponseBody
public String testValue(@PathVariable("id") String id, @RequestHeader("User-Agent") String userAgent,
@RequestParam(value = "name", defaultValue = "null", required = false) String name, @CookieValue("JSESSIONID") String jsessionid) {
return userAgent + "<br>" + jsessionid + "<br>" + id + "<br>" + name;
}
/**
* 使用Servlet API对象作为入参
*
* @param request
* @param response
*/
@RequestMapping("/request")
public void req(HttpServletRequest request, HttpServletResponse response) {
Assert.notNull(request, "请求不能为空");
response.setStatus(200);
}
/**
* 使用IO对象作为入参
*
* @param os
* @throws IOException
*/
@RequestMapping("/stream/{path}")
public void img(OutputStream os, @PathVariable("path") String path) throws IOException {
ClassPathResource res = new ClassPathResource("/spring-mvc.xml");
FileCopyUtils.copy(res.getInputStream(), os);
}
/**
* 表单提交 示例,bean的属性 自动 装配 <br>
* 指定方法类型:method = RequestMethod.POST <br>
* http://localhost/test/rest/test/body?name=starzou&password=123
*
* @param user
* @return
*/
@RequestMapping(value = "/body")
@ResponseBody
public Object body(User user) {
return user;
}
/**
* httpEntity.getHeaders() 得到所有请求头
*
* @param httpEntity
* @return
*/
@RequestMapping("/http")
@ResponseBody
public Object http(HttpEntity<String> httpEntity) {
return httpEntity.getHeaders().entrySet();
}
/**
* 调用 业务层 方法示例
*
* @return
*/
@RequestMapping("/date")
@ResponseBody
public String date() {
return testServiceImpl.date();
}
/**
* Spring MVC将匹配方法参数名URI模板变量名称
*
* @param x
* @param y
* @return
*/
@RequestMapping("/var/{x}/{y}")
@ResponseBody
public String var(@PathVariable String x, @PathVariable String y) {
return x + "<br>" + y;
}
/**
* 测试 mybaits generator生成的 dao model,进行 数据库操作
*
* @return
*/
@RequestMapping("/tt/add")
@ResponseBody
public Object testAdd() {
com.eduoinfo.finances.bank.web.model.Test test = new com.eduoinfo.finances.bank.web.model.Test();
final long currentTimeMillis = System.currentTimeMillis();
test.setTname("tname : " + currentTimeMillis);
test.setTdate(new Date());
testMapper.insert(test);
return test;
}
/**
* 数据库 查询
*
* @return
*/
@RequestMapping("/tt/select")
@ResponseBody
public Object testSelect() {
final List<Test> list = testMapper.selectByExample(null);
return list;
}
/**
* 测试 spring mvc 返回 json , 封装 Json 格式数据, 减少 类型转换
*
* @return
*/
@RequestMapping("/json")
@ResponseBody
public JsonResult<Object[]> returnJson() {
// 实际情况 下 String,可能是一个 自定义的Java 类,比如 User , 通常是在 数据库查询
List<String> data = new ArrayList<>();
Set<String> data2 = new HashSet<>();
Map<String, String> data3 = new HashMap<>();
int i = 0;
while (i < 10) {
String value = "data-" + (++i);
data.add(value);
data2.add(value);
data3.put(value, value);
}
// 组装 查询的 结果 , 添加消息 和 是否成功的标识
JsonResult<List<String>> jsonResult = new JsonResult<>(data, "This is a message.", true);
JsonResult<Set<String>> jsonResult2 = new JsonResult<>(data2, "This is a message.", true);
JsonResult<Map<String, String>> jsonResult3 = new JsonResult<>(data3, "This is a message.", true);
// 复杂一点的 封装
Object[] objs = { jsonResult, jsonResult2, jsonResult3 };
JsonResult<Object[]> jsonObj = new JsonResult<Object[]>(objs);
return jsonObj;
}
}
Spring 中Control示例
最新推荐文章于 2023-03-09 12:24:42 发布