蒙牛网站是谁做的,怎么做网站支付,网站消耗流量,wordpress修改轮播图前言
SpringMVC#xff08;2#xff09;——controller方法参数与html表单对应#xff08;请求参数的绑定#xff09;
上篇博客我们提到了controller方法的参数与html表单之间的对应关系
但是这种对应关系有很多缺点#xff1a;
传递参数只能放在request的body当中2——controller方法参数与html表单对应请求参数的绑定
上篇博客我们提到了controller方法的参数与html表单之间的对应关系
但是这种对应关系有很多缺点
传递参数只能放在request的body当中url参数无法获取传递参数无法设置哪些不可为空哪些可以为空
SpringMVC为了简化这些繁琐的操作为前后端字段的转换提供了一些很方便的注解
RequestParam注解
Target(ElementType.PARAMETER)
Retention(RetentionPolicy.RUNTIME)
Documented
public interface RequestParam {AliasFor(name)String value() default ;AliasFor(value)String name() default ;boolean required() default true;String defaultValue() default ValueConstants.DEFAULT_NONE;}可以设置是否必传默认值还有对应前端的字段名
value/name属性
作用相同。指定前端form表单传递的参数的名称
required属性
默认true表示必传前端若想调用此controller的此方法必须保证此字段有值
设置为false则表示可以不传
defaultValue属性
表示默认值只有required属性为false时才会生效而且只能指定为字符串类型
demo
Controller
RequestMapping(/paramAnno)
public class ParamAnnoController {RequestMapping(/requestParam)public String requestParamAnno(RequestParam(value name) String username,RequestParam(required false) Integer age,RequestParam(value id) Long id) {System.out.println(RequestParam注解使用生效);System.out.println(name: username ,age: age ,id: id);return suc;}jsp页面的表单部分
h3RequestParam测试/h3form action/paramAnno/requestParam methodpost姓名input typetext namename/br/年龄input typetext nameage/br/idinput typetext nameid/br/input typesubmit value提交/
/formRequestBody注解
Target(ElementType.PARAMETER)
Retention(RetentionPolicy.RUNTIME)
Documented
public interface RequestBody {/*** Whether body content is required.* pDefault is {code true}, leading to an exception thrown in case* there is no body content. Switch this to {code false} if you prefer* {code null} to be passed when the body content is {code null}.* since 3.2*/boolean required() default true;}只有一个required属性表示可传可不传
表示前端传一个js对象给后端js对象中可以包含各种属性
如果想要对应后端的实体类对象需要前端使用js封装为js对象发送给后端form表单做不到发送js对象
我们这里用String代替 PostMapping(value /requestBody)public String requestBodyAnno(RequestBody String body) {System.out.println(RequestBody注解使用生效);System.out.println(body);return suc;}form action/paramAnno/requestBody methodpostf1input typetext namefun.f1/br/f2input typetext namefun.f2/br/f3input typetext namefun.f3/br/list0.f1:input typetext namelist[0].f1/br/list0.f2:input typetext namelist[0].f2/br/list0.f3:input typetext namelist[0].f3/br/list1.f1:input typetext namelist[1].f1/br/list1.f2:input typetext namelist[1].f2/br/list1.f3:input typetext namelist[1].f3/br/birthday:input typetext namebirthday/br/map.f1:input typetext namemap[fun].f1/br/map.f2:input typetext namemap[fun].f2/br/map.f3:input typetext namemap[fun].f3/br/input typesubmit value提交/
/formPathVariable注解
Target(ElementType.PARAMETER)
Retention(RetentionPolicy.RUNTIME)
Documented
public interface PathVariable {/*** Alias for {link #name}.*/AliasFor(name)String value() default ;/*** The name of the path variable to bind to.* since 4.3.3*/AliasFor(value)String name() default ;/*** Whether the path variable is required.* pDefaults to {code true}, leading to an exception being thrown if the path* variable is missing in the incoming request. Switch this to {code false} if* you prefer a {code null} or Java 8 {code java.util.Optional} in this case.* e.g. on a {code ModelAttribute} method which serves for different requests.* since 4.3.3*/boolean required() default true;}属性作用同上这里不再赘述
此注解适用于js基本类型不适用于js对象
作用是直接在请求的url中拼接我们想要的内容比如数字字符串等
需要在RequestMapping中添加{}指定拼接的位置进行使用 PostMapping(/pathVariable/{id})public String pathVariableAnno(PathVariable Long id) {System.out.println(PathVariable注解使用生效);System.out.println(id);return suc;}h3PathVariable测试/h3form action/paramAnno/pathVariable methodpostinput typesubmit value提交/
/formRequestHeader
作用获取指定请求头的值
请求头包含很多属性 PostMapping(value /requestHeader)public String requestHeaderAnno(RequestHeader(value content-type) String contentType,RequestHeader(Accept) String accept) {System.out.println(RequestHeader注解使用生效);System.out.println(contentType);System.out.println(accept);return suc;}h3RequestHeader测试/h3form action/paramAnno/requestHeader methodpostinput typesubmit value提交/
/formCookieValue注解
后端获取前端指定cookie的值 PostMapping(/cookieSession)public String cookieSessionAnno(CookieValue(value JSESSIONID) String cookie) {System.out.println(CookieValue注解使用生效);System.out.println(cookie);return suc;}h3CookieSession测试/h3form action/paramAnno/cookieSession methodpostinput typesubmit value提交/
/formJSESSIONID其实就是session是一种标记http请求状态并且安全的浏览器存储
这里的数据都是经过加密的