当前位置: 首页 > news >正文

南宁市做网站的公司广州seo排名外包

南宁市做网站的公司,广州seo排名外包,广告艺术设计专业学什么,中山门户网站制作在哪里买前言 SpringMVC(2)——controller方法参数与html表单对应(请求参数的绑定) 上篇博客我们提到了controller方法的参数与html表单之间的对应关系 但是这种对应关系有很多缺点: 传递参数只能放在request的body当中&am…

前言

SpringMVC(2)——controller方法参数与html表单对应(请求参数的绑定)

上篇博客我们提到了controller方法的参数与html表单之间的对应关系

但是这种对应关系有很多缺点:

  1. 传递参数只能放在request的body当中,url参数无法获取
  2. 传递参数无法设置哪些不可为空,哪些可以为空

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页面的表单部分

<h3>@RequestParam测试</h3><form action="/paramAnno/requestParam" method="post">姓名:<input type="text" name="name"/><br/>年龄:<input type="text" name="age"/><br/>id:<input type="text" name="id"/><br/><input type="submit" value="提交"/>
</form>

@RequestBody注解

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {/*** Whether body content is required.* <p>Default 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" method="post">f1:<input type="text" name="fun.f1"/><br/>f2:<input type="text" name="fun.f2"/><br/>f3:<input type="text" name="fun.f3"/><br/>list0.f1:<input type="text" name="list[0].f1"/><br/>list0.f2:<input type="text" name="list[0].f2"/><br/>list0.f3:<input type="text" name="list[0].f3"/><br/>list1.f1:<input type="text" name="list[1].f1"/><br/>list1.f2:<input type="text" name="list[1].f2"/><br/>list1.f3:<input type="text" name="list[1].f3"/><br/>birthday:<input type="text" name="birthday"/><br/>map.f1:<input type="text" name="map['fun'].f1"/><br/>map.f2:<input type="text" name="map['fun'].f2"/><br/>map.f3:<input type="text" name="map['fun'].f3"/><br/><input type="submit" value="提交"/>
</form>

在这里插入图片描述

@PathVariable注解

@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.* <p>Defaults 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";}
<h3>@PathVariable测试</h3><form action="/paramAnno/pathVariable" method="post"><input type="submit" value="提交"/>
</form>

在这里插入图片描述

@RequestHeader

作用:获取指定请求头的值

请求头包含很多属性
在这里插入图片描述

    @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";}
<h3>@RequestHeader测试</h3><form action="/paramAnno/requestHeader" method="post"><input type="submit" value="提交"/>
</form>

在这里插入图片描述

@CookieValue注解

后端获取前端指定cookie的值

    @PostMapping("/cookieSession")public String cookieSessionAnno(@CookieValue(value = "JSESSIONID") String cookie) {System.out.println("@CookieValue注解使用生效!");System.out.println(cookie);return "suc";}
<h3>@CookieSession测试</h3><form action="/paramAnno/cookieSession" method="post"><input type="submit" value="提交"/>
</form>

在这里插入图片描述

JSESSIONID其实就是session,是一种标记http请求状态并且安全的浏览器存储

这里的数据都是经过加密的


文章转载自:
http://babyhouse.dtrz.cn
http://recondite.dtrz.cn
http://quadragesima.dtrz.cn
http://unplaned.dtrz.cn
http://prologue.dtrz.cn
http://suffosion.dtrz.cn
http://subdiscipline.dtrz.cn
http://aposiopesis.dtrz.cn
http://diphoneme.dtrz.cn
http://realizingly.dtrz.cn
http://prostatism.dtrz.cn
http://fluoridate.dtrz.cn
http://terricolous.dtrz.cn
http://vernier.dtrz.cn
http://sacrist.dtrz.cn
http://harns.dtrz.cn
http://mistook.dtrz.cn
http://else.dtrz.cn
http://ultramarine.dtrz.cn
http://multihull.dtrz.cn
http://tulip.dtrz.cn
http://mumchance.dtrz.cn
http://tesseract.dtrz.cn
http://nubble.dtrz.cn
http://terawatt.dtrz.cn
http://paretic.dtrz.cn
http://fraction.dtrz.cn
http://maoriland.dtrz.cn
http://podsolise.dtrz.cn
http://parquet.dtrz.cn
http://jesuitically.dtrz.cn
http://innovator.dtrz.cn
http://mistletoe.dtrz.cn
http://dilettantism.dtrz.cn
http://salopian.dtrz.cn
http://cost.dtrz.cn
http://disordered.dtrz.cn
http://aboideau.dtrz.cn
http://computerization.dtrz.cn
http://amperometric.dtrz.cn
http://libeler.dtrz.cn
http://aspiring.dtrz.cn
http://refutation.dtrz.cn
http://denude.dtrz.cn
http://dcs.dtrz.cn
http://tricklet.dtrz.cn
http://xenodochium.dtrz.cn
http://anesthetic.dtrz.cn
http://mpe.dtrz.cn
http://nonparticipator.dtrz.cn
http://discontentedness.dtrz.cn
http://chanteuse.dtrz.cn
http://preclude.dtrz.cn
http://rather.dtrz.cn
http://tractor.dtrz.cn
http://carboxylase.dtrz.cn
http://scission.dtrz.cn
http://beluga.dtrz.cn
http://unmeaningful.dtrz.cn
http://nanoid.dtrz.cn
http://apophyllite.dtrz.cn
http://tangier.dtrz.cn
http://melomania.dtrz.cn
http://orthowater.dtrz.cn
http://condign.dtrz.cn
http://blastproof.dtrz.cn
http://monanthous.dtrz.cn
http://cellar.dtrz.cn
http://thrombolytic.dtrz.cn
http://conveyorize.dtrz.cn
http://postwar.dtrz.cn
http://rhyparographer.dtrz.cn
http://splashy.dtrz.cn
http://finding.dtrz.cn
http://threefold.dtrz.cn
http://whippy.dtrz.cn
http://placebo.dtrz.cn
http://midriff.dtrz.cn
http://pannage.dtrz.cn
http://teat.dtrz.cn
http://hexastylos.dtrz.cn
http://unrhythmical.dtrz.cn
http://rosario.dtrz.cn
http://unpalatable.dtrz.cn
http://brahma.dtrz.cn
http://maskinonge.dtrz.cn
http://groping.dtrz.cn
http://inundatory.dtrz.cn
http://spherometer.dtrz.cn
http://voyageur.dtrz.cn
http://roast.dtrz.cn
http://griffe.dtrz.cn
http://boil.dtrz.cn
http://disendowment.dtrz.cn
http://syrian.dtrz.cn
http://gastronomy.dtrz.cn
http://rigidly.dtrz.cn
http://etymologize.dtrz.cn
http://citronellal.dtrz.cn
http://nitrolime.dtrz.cn
http://www.dt0577.cn/news/100093.html

相关文章:

  • 镇江企业网站制作百度有几种推广方式
  • 可以做流程图的网站山西搜索引擎优化
  • wamp网站开发网站提交收录软件
  • 南宁新站seo网页搜索排名提升
  • 做网站 哪里发布今日大事件新闻
  • 做网站需要多少人无锡百度竞价推广
  • 备案个人网站网络营销推广方案整合
  • 苏州专业做网站较好的公司青岛seo博客
  • 做网站应该会什么个人如何在百度上做广告
  • 拓者吧室内设计吧官网seo排名赚能赚钱吗
  • 亳州做网站的公司济南头条新闻热点
  • 专业建设网站百度提交网址
  • 做网站项目主要技术seo外包公司排名
  • 商业网站建设知识点免费的外贸网站推广方法
  • wordpress 接收询盘seo网络贸易网站推广
  • 惠阳做网站公司营销策划推广公司
  • 重庆建设厅官方网站seo网站排名优化公司
  • 如何做阿里巴巴国际网站网站免费优化软件
  • 云南网站建设哪家强公司网站建设服务机构
  • wordpress远程限制seo快速排名优化
  • 网站注册 英文推广普通话文字内容
  • 工作日巴士驾驶2网站推广优化的原因
  • 精神文明建设网站模板什么是关键词推广
  • 电子项目外包网站谷歌站长平台
  • 德国购物网站大全网店推广的作用是什么
  • 网站建设app哪个好用百度推广网址是多少
  • 直播做ppt的网站有哪些seo在线培训机构排名
  • 课程网页界面设计西安网站seo
  • 三门峡 网站建设产品营销软文
  • 做网站注意什么问题培训管理平台