Spring 中Control示例
来源:互联网 发布:整形网站源码 编辑:程序博客网 时间:2024/05/16 15:01
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; } }
0 0
- Spring 中Control示例
- MFC中Spin Control,Slider Control,Edit Control控件结合使用示例
- Spring中Bean配置示例
- spring中定时器编写示例
- spring 项目中集成 Protocol Buffers 示例
- spring 项目中集成 Protocol Buffers 示例
- spring中AOP 注解开发示例详解
- Tab Control控件示例教程
- C#中Control的作用,及应用示例(清空TextBox)
- C#中Control的作用,及应用示例(清空TextBox)
- Spring注解 @Component,@Service,@Control
- Spring ioc(Inversion of Control )
- Control study -> Drag拖放数据操作示例
- Control study -> Drag拖放数据操作示例
- Control study -> Drag拖放数据操作示例
- Ajax Control Toolkit:AutoComplete控件示例
- Spring Mvc——Controller中常规方法示例
- Spring Mvc——Controller中常规方法示例
- Redis常见七种使用
- java : Maven in action (Maven 3 实战) 中文目录
- 关于C语言的malloc函数以及内存泄漏的问题
- HTML5基础——文本标记
- Java自学心得—1
- Spring 中Control示例
- 小白程序媛的进击之路(面向对象)
- 【Java】数组--数三退一实例
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- Mybatis 学习之API
- 欢迎使用CSDN-markdown编辑器
- Android Studio中有六种依赖
- Scrapy运行ImportError: No module named win32api错误
- makefile的使用