网站营销的优势,商务网站建设个人总结,中国网站建设集团,烈士陵园网站建设方案百度文库目录 前后端数据交互RequestMapping注解基于RequestMapping注解设置接口的请求方式RequestMapping注解的常用属性一个方法配置多个接口method属性params属性headers属性consumes属性produces属性 SpringMVC中的参数传递默认单个简单参数默认多个简单参数默认参数中有基本数据类… 目录 前后端数据交互RequestMapping注解基于RequestMapping注解设置接口的请求方式RequestMapping注解的常用属性一个方法配置多个接口method属性params属性headers属性consumes属性produces属性 SpringMVC中的参数传递默认单个简单参数默认多个简单参数默认参数中有基本数据类型RequestParam注解设置参数RequestParam注解设置参数非必传RequestParam注解设置参数名称RequestParam注解设置参数默认值 传对象/Map传数组传List集合传JSON SpringMVC响应数据Spring MVC提供了多种方式输出模型数据思考如果是Ajax请求期望服务端响应的不是页面而是数据应该怎么处理ResponseBody生效范围RestController SpringMVC参数传递时的Rest风格RESTRepresentational State Transfer表现形式状态转换优点分类代码示例对应的访问方式查询删除新增修改 前后端数据交互
Spring MVC框架是控制层框架主要负责处理前后端之间的数据交互工作包括从请求中获取入参数据并向前端返回处理结果。Spring MVC框架是如何处理数据交互问题的
RequestMapping注解
RequestMapping注解负责把不同的请求映射到对应的控制器方法上。RequestMapping注解不仅可以作用于控制器的方法上还可以标注到控制器类上。RequestMapping注解添加到Controller类上时表示当前控制器下所有接口的访问路径有相同的前缀
基于RequestMapping注解设置接口的请求方式 在Web项目开发中通常会使用GET类型请求访问查询接口使用POST类型请求访问保存方法RequestMapping注解可以为接口设置访问类型 Controller
public class HelloController {RequestMapping(value /hello,method {RequestMethod.GET,RequestMethod.POST})public String hello(){return index;}
}除了使用method属性设置接口访问类型外SpringMVC框架还提供了GetMapping、PostMapping等注解实现类似功能。如下接口如果使用POST以外的请求类型进行访问就会报错 Controllerpublic class HelloController {PostMapping(/hello)public String hello(){return index;}}浏览器url访问默认是get请求的
RequestMapping注解的常用属性
属性名描述value指定请求的实际访问地址默认RequestMapping(“url”)的值url即为value的值。指定的地址可以是 URI Template 模式。method指定请求的method类型主要有 GET、POST、DELETE、PUT等params指定request中必须包含某些参数值包含才让该方法处理请求。headers指定request中必须包含某些指定的header值包含才能让该方法处理请求。consumes指定处理请求的提交内容类型Content-Type例如application/json, text/html;produces指定返回的内容类型当且仅当request请求头中的(Accept)类型中包含该指定类型才返回
一个方法配置多个接口
访问//hello/hi都是访问这个hello方法
Controller
public class HelloController {RequestMapping(value {/,/hello,/hi})//RequestMapping(path {/,/hello,/hi})// path也可以public String hello(){return index;}
}method属性
如果没有指定method属性则表示任何形式的请求都可以访问该接口如果设置了method的值就只能支持设置值的请求方式其它请求方式不支持就会报405错误 – Method Not Allowed可以指定一种或者多种(数组形式)请求方式 //RequestMapping(value /hello,method {RequestMethod.POST,RequestMethod.GET})RequestMapping(value /hello,method RequestMethod.POST)public String hello(){return index;}params属性
指定request中必须包含某些参数值包含才让该方法处理请求。 //请求中的参数who值为world才执行该方法//RequestMapping(value /hello,params {whoworld})//请求中的参数who值不为world才执行该方法RequestMapping(value /hello,params {who!world})public String hello(String who){System.out.println(hello who);return index;}headers属性
指定request请求作用域中必须包含某些指定的header值包含才能让该方法处理请求。 RequestMapping(value /hello,headers{context-typetext/plain,context-typetext/html})public String hello(){return index;}上述访问如果请求头中不包含context-typetext/plain,context-typetext/html这两个属性那么就不能访问到该方法报404错误。
consumes属性 RequestMapping(value /hello, method RequestMethod.POST, consumesapplication/json)public String hello(){return index;}方法仅处理request Content-Type为“application/json”类型的请求。
produces属性 RequestMapping(value /hello, method RequestMethod.GET, producesapplication/json)ResponseBodypublic String hello(){return index;}方法仅处理request请求中Accept头中包含了application/json的请求同时暗示了返回的内容类型为application/json;
SpringMVC中的参数传递
在前面的课程中使用Servlet接收前端请求时通常会用到HttpServletRequest对象接收参数代码如下String realNamerequest.getParameter(“realName”);
Integer idrequest.getParameter(“id”);每一个参数都需要编写代码进行接收且需要手动转换参数的类型判断参数值是否为空给开发人员带来了很大的工作量。怎么解决Spring MVC框架提供了RequestParam注解可以自动完成以上绝大部分工作。
默认单个简单参数 RequestMapping(value /hello)public String hello(String who){System.out.println(hello who);return index;}此时who这个参数可传可不传但如果传参参数名必须是who
默认多个简单参数 RequestMapping(value /hello)public String hello(String who,String count){System.out.println(hello who , count);return index;}此时who、count都可传可不传但如果传参参数名必须是who和count顺序无所谓
默认参数中有基本数据类型 RequestMapping(value /hello)public String hello(int count){System.out.println(hello, count);return index;}按理说count可传可不传但是不传参数则形参默认值为null而这个count是几本数据类型无法将null转换因此就会报错。因此接口中不要用基本数据类型作为参数尽量使用包装类
RequestParam注解设置参数 RequestMapping(value /hello)public String hello(RequestParam String who){System.out.println(hello who);return index;}此时访问这个接口时就必须传参了并且参数名只能是who
RequestParam注解设置参数非必传
required属性默认值true表示必传false表示非必传 RequestMapping(value /hello)public String hello(RequestParam(required false) String who){System.out.println(hello who);return index;}RequestParam注解设置参数名称
name属性 RequestMapping(value /hello)public String hello(RequestParam(name paramName,required false) String who){System.out.println(hello who);return index;}此时访问这个接口时就必须传参了并且参数名只能是paramName
RequestParam注解设置参数默认值
defaultvalue属性 RequestMapping(value /hello)public String hello(RequestParam(defaultValue springmvc) String who){System.out.println(hello who);return index;}此时访问这个接口时如果不传who那么who就会用springmvc作为默认值
传对象/Map
通常传对象都是用Post请求或者Put请求 PostMapping(value /hello)public String hello(User user){System.out.println(hello: user);return index;}此时传参时只需要让参数名与对象的属性名相同就可以将参数映射到对象中如果是form表单只需要让表单各组件的name值与对象属性名相同即可
传数组 GetMapping(value /hello)public String hello(String[] hobbys){for (String hobby : hobbys) {System.out.println(hobby);}return index;}此时传参时直需要将多个参数值用逗号分割即可
传List集合
需要加上RequestParam才行否则报错 GetMapping(value /hello)public String hello(RequestParam ListString hobbys){for (String hobby : hobbys) {System.out.println(hobby);}return index;}传JSON
示例 GetMapping(value /hello)public String hello(RequestBody ListUser userList){userList.forEach(System.out::println);return index;}配置转换适配器否则会报错HttpMediaTypeNotSupportedException: Content type application/json not supp !--处理器适配器 --bean classorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapterproperty namemessageConverterslistbean classorg.springframework.http.converter.json.MappingJackson2HttpMessageConverter/bean/list/property/bean测试
SpringMVC响应数据
Spring MVC提供了多种方式输出模型数据
使用ModelAndView对象 GetMapping(value /hello)public ModelAndView hello(){ModelAndView mv new ModelAndView();mv.setViewName(index);//设置返回的逻辑视图名mv.addObject(msg,hello world);//设置后端向前端传递的数据return mv;}使用Model对象推荐 在Model中增加模型数据若不指定key则默认使用对象的类型作为key GetMapping(value /hello)public String hello(Model model){model.addAttribute(msg,Hello,SpringMVC);return index;}使用Map对象 Model其实就是一个Map的数据结构可以使用Map作为处理方法入参 返回的Map必须放在参数中作为形参可以改变内容但不能指向新的Map GetMapping(value /hello)public String hello(MapString,Object returnMap){returnMap.put(msg,Hello,SpringMVC);return index;}思考如果是Ajax请求期望服务端响应的不是页面而是数据应该怎么处理
使用ResponseBody注解 GetMapping(value /hello)ResponseBodypublic User hello(){User user new User();user.setUserName(周杰伦);user.setUserCode(zjl);return user;}配置添加消息转换器 mvc:annotation-drivenmvc:message-convertersbean classorg.springframework.http.converter.StringHttpMessageConverterproperty namesupportedMediaTypeslistvalueapplication/json;charsetUTF-8/value/list/property/beanbean classcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverterproperty namesupportedMediaTypeslistvaluetext/html;charsetUTF-8/valuevalueapplication/json/value/list/propertyproperty namefeatureslist!-- Date的日期转换器 --valueWriteDateUseDateFormat/value/list/property/bean/mvc:message-converters/mvc:annotation-drivenResponseBody生效范围
加在方法上只对该方法生效加在Controller类上则该Controller中的所有方法都不再返回页面而是返回数据Controller
ResponseBody
public class HelloController {...
}RestController
作用不再解释了直接看源码一目了然
SpringMVC参数传递时的Rest风格
RESTRepresentational State Transfer表现形式状态转换
传统风格资源描述形式http://localhost/user/getById?id1http://localhost/user/saveUserREST风格描述形式http://localhost/user/1http://localhost/user
优点
隐藏资源的访问行为无法通过地址得知对资源是何种操作书写简化
分类
method操作类型GET查询POST新增PUT修改DELETE删除
代码示例
package cn.smbms.controller;import cn.smbms.pojo.User;
import org.springframework.web.bind.annotation.*;/*** author: zjl* datetime: 2024/4/19* desc:*/
RestController
RequestMapping(/user)
public class UserController {GetMapping(/{id})public Integer getById(PathVariable Integer id) {System.out.println(根据id查询 id);return id;}PostMapping(/change)public User insert(RequestBody User user){System.out.println(新增用户 user);return user;}PutMapping(/change)public User update(RequestBody User user){System.out.println(更新用户 user);return user;}DeleteMapping(/{id})public Integer delete(PathVariable Integer id){System.out.println(删除用户 id);return id;}
}对应的访问方式
查询
http://localhost:9090/smbms/user/1
删除
http://localhost:9090/smbms/user/1
新增
http://localhost:9090/smbms/user/change
修改
http://localhost:9090/smbms/user/change