Spring controller层请求数据和响应数据的简单方法

记录一些很基本的使用方法。

一、GET请求传参方法:

1.方法一:把参数传到?之后 使用注解@RequestParam

java 复制代码
// 假如传值了current和limit  /students?current=1&limit=20
@RequestMapping(value = "/students", method = RequestMethod.GET)
@ResponseBody
// name代表传的参数名,require代表不传这个值也行,defaultvalue代表默认值
// 默认current和limit参数是int类型
public String getStudents(@RequestParam(name = "current", required = false, defaultValue = "1") int current,
                          @RequestParam(name = "limit", required = false, defaultValue = "1") int limit) {
    return "some students:" + current + " " + limit;
}

2.方法二:传到路径当中 使用注解@PathVariable指定

java 复制代码
// /student/123      查找一个学生
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id") int id) {
    return "a student" + " " + id;
}

二、POST请求传参方法:默认使用表单提交,获取表单中字段的name属性

java 复制代码
// 这里表单传入了一个学生的姓名和年龄  
@RequestMapping(value = "/student", method = RequestMethod.POST)
@ResponseBody
public String saveString(String name, int age) {
    System.out.println("name:" + name);
    return "success" + " " + name + " " + age;
}

三、响应HTML数据,分别使用ModelAndView 和Model

使用ModelAndView :

java 复制代码
// 这里是model和view都封装在一个对象中
// 不加@ResponseBody  返回的就是html;  加了就返回其他对象,比如json
@RequestMapping(value = "/teacher", method = RequestMethod.GET)
public ModelAndView getTeacher(ModelAndView modelAndView) {
    modelAndView.addObject("name","ym");
    modelAndView.addObject("age", 21);
    // 指定一下模板目录  会默认在templates目录下  下面的/view也就是view.html
    modelAndView.setViewName("/demo/view");
    return modelAndView;
}

使用Model:

java 复制代码
// model与view分开
@RequestMapping(value = "/school", method = RequestMethod.GET)
public String getSchool(Model model) {
    model.addAttribute("name", "北京大学");
    model.addAttribute("age", 100);
    return "/demo/view";
}

四、响应JSON数据,主要针对异步请求(ajax等等)

java 复制代码
// Java对象返回给浏览器,解析为JS对象,就用JSON完成前面的操作 java->json->js对象
@RequestMapping(value = "/emp", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getEmp() {
    Map<String, Object> emp = new HashMap<>();
    emp.put("name", "ljm");
    emp.put("salary", 18000);
    emp.put("age", 21);
    return emp;
}

五、cookie示例

分别编写了设置cookie和获取cookie的方法,

java 复制代码
@GetMapping("/cookie/set")
@ResponseBody
// 把cookie放在response响应里
public String setCookie(HttpServletResponse response) {
    // 创建cookie    这里使用uuid随机创建值
    Cookie cookie = new Cookie("code", CommunityUtil.generateUUID());
    // 设置cookie生效的范围  也就是说只在访问下面这个链接时生效
    cookie.setPath("/community/alpha");
    // 默认关闭浏览器就失效  但可以设置时间保证cookie的存活时间
    cookie.setMaxAge(60 * 10);
    // 发送cookie
    response.addCookie(cookie);
    return "set cookie";
}

@RequestMapping("/cookie/get")
@ResponseBody
// "code"是上面设置的值
public String getCookie(@CookieValue("code") String code) {
    System.out.println("code:" + code);
    return "get cookie";
}

六、session示例

java 复制代码
@RequestMapping("/session/set")
@ResponseBody
public String setSession(HttpSession session) {
    session.setAttribute("id", 1);
    session.setAttribute("name", "Test");
    return "set session";
}
@RequestMapping("/session/get")
@ResponseBody
public String getSession(HttpSession session) {
    System.out.println(session.getAttribute("id"));
    System.out.println(session.getAttribute("name"));
    return "get session";
}
相关推荐
鹏多多4 分钟前
基于Vue3+TS的自定义指令开发与业务场景应用
前端·javascript·vue.js
江城开朗的豌豆13 分钟前
Redux 与 MobX:我的状态管理选择心路
前端·javascript·react.js
Cosolar18 分钟前
前端如何实现VAD说话检测?
前端
CodeSheep36 分钟前
当了leader才发现,大厂最想裁掉的,不是上班总迟到的,也不是下班搞失联的,而是经常把这3句话挂在嘴边的
前端·后端·程序员
吃饺子不吃馅41 分钟前
✨ 你知道吗?SVG 里藏了一个「任意门」——它就是 foreignObject! 🚪💫
前端·javascript·面试
IT_陈寒1 小时前
Python开发者必须掌握的12个高效数据处理技巧,用过都说香!
前端·人工智能·后端
gnip9 小时前
企业级配置式表单组件封装
前端·javascript·vue.js
一只叫煤球的猫10 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
excel11 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼11 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github