好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringMVC 接收前端传递的参数四种方式小结

SpringMVC 接收前端传递的参数四种方式

@RequestParam注解 @PathVariable注解 SpringMVC的自动解析参数 SpringMVC的@RequestBody注解

@RequestParam 获取注解

?

1

2

3

get/post  url => "xx/user?id=1"     action =>

public String User( @RequestParams (name= "id" ) Long id ){

}

@RequestParam定义的参数 会自动解析为 方法定义的类型。

?

1

@RequestParams (name= "id" ) Long id )(通过postman模拟post请求)

@PathVariable获取注解

?

1

get/post  url => "xx/user/1"     action => public String User( @PathVariable (name= "id" ) Long id){}

@PathVariable必须通过正则指定对应的类型 只有当url指定为数字,方法参数定义为数字类型才不会报错。比如:(可以通过其他正则限制url,只有符合条件的url才会映射到对应的action,否则不会找到对应的action)

?

1

2

@RequestMapping ( "/user/{id:\\d}" )

public String User( @PathVariable (name= "id" ) Long id){}

SpringMVC,可以不设置任何注解即可接收参数

比如

?

1

2

3

4

5

@GetMapping ( "/category" )

    public String category( Long id) {

     System.out.println(id);

     return "post/category" ;

}

可以通过 /category 访问 ,也可以通过 /category?id=1 访问

SpringMVC,也可以自动包装成对象

url /category?title=测试 或者 /category 都能访问到目标资源

?

1

2

3

4

5

@GetMapping ( "/category" )

public String category( MPost post ) {

     System.out.println(post.getTitle());

     return "post/category" ;

}

@RequestBody 用来接收数组或者复杂对象

(必须将参数放在requestbody中,放在url并不会被解析,哪怕请求方式是post)

?

1

2

3

4

5

6

url  => /category  requestbody =>{ "id" : 1 }

     @PostMapping ( "/category" )

     public String category( @RequestBody Post post ) {

         System.out.println(post.getTitle());

         return "post/category" ;

     }

若为对象数组,将方法参数改为 @RequestBody List<Post> post 即可

直接输入 /category并不会找到对应的action

SpringMVC的自动封装(不传参也能进入)

@RequestParam (必须传参,但可以手动设置为false) @PathVariable (符合设定的正则表达式才允许进入,而且不能为空)

对比可知,主要是为了url提供更加严格的限制,以防止一些其他url进入该action。

提供复杂的接受参数的方式@RequestBody ,但必须将参数放置在@RequestBody中

针对PathVariable 需要注意的是参数中包含特殊字符的问题,可能导致参数不全。

对于各种请求方式,验证一下当前用户,对url进行加密 是有必要的。(尤其是关键数据)

SpringMVC接收不到前端传递的参数原因

在学习SpringMvc的时候遇到了一个问题,后台一直接收不到前台传递过来的参数,耽误了好长时间终于找到了原因,写篇博客记录下这个坑,嘤嘤嘤 --__–

代码清单

使用SpringMvc接受前台传递的参数非常简单,只要参数名和前台表单中的名称一致即可,我弄得是一个文件上传的例子,所以看下我的前台页面

?

1

2

3

4

5

6

7

8

9

10

< body >

<!-- enctype="multipart/form-data"在文件上传时加入,编码类型,其值默认是application/x-www-form-urlencoded -->

< form action = "testFileUpload" method = "post" enctype = "multipart/form-data" >

File: < input type = "file" name = "file" />

Desc: < input type = "text" name = "desc" />

< input type = "submit" value = "Submit" />

</ form >

< br >< br >

< a   href = "emps" rel = "external nofollow" >List All Employees</ a >

</ body >

下面是SpringMvc的控制器

?

1

2

3

4

5

6

7

8

9

10

11

@Controller

public class springMVCTest {

  @RequestMapping ( "/testFileUpload" )

  public String testFileUpload( @RequestParam ( "desc" ) String desc,

    @RequestParam ( "file" ) MultipartFile file) throws IOException {

   System.out.println( "desc: " + desc);

   System.out.println( "originalFilename: " + file.getOriginalFilename());

   System.out.println( "inputStream: " + file.getInputStream()); 

   return "success" ;

  }

}

接着是web.xml文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!-- 配置DispatcherServlet -->

  <!-- SpringMvc会根据servlet-name配置,找到/WEB-INF/dispatcher-servlet.xml作为配置文件载入Web工程中 -->

  < servlet >

   < servlet-name >springDispatcherServlet</ servlet-name >

   < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >

   < init-param >

    < param-name >contextConfigLocation</ param-name >

    < param-value >classpath:springmvc.xml</ param-value >

   </ init-param >

   < load-on-startup >1</ load-on-startup >

  </ servlet >

  < servlet-mapping >

   < servlet-name >springDispatcherServlet</ servlet-name >

   < url-pattern >/</ url-pattern >

  </ servlet-mapping >

** 然后是SpringMvc的配置文件**

?

1

2

3

4

5

6

7

8

9

<!-- 配置自动扫描的包 -->

  < context:component-scan base-package = "com.zgz.springmvc.crud" ></ context:component-scan >

  < context:component-scan base-package = "com.zgz.springmvc.test" ></ context:component-scan >

 

  <!-- 配置视图解析器 -->

  < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >

   < property name = "prefix" value = "/WEB-INF/views/" ></ property >

   < property name = "suffix" value = ".jsp" ></ property >

  </ bean >

之后坑就来了,由于是文件上传,所以需要在Spring MVC的配置文件中添加multipartResolver,添加就添加呗,于是我就加上了下面这一段代码:

?

1

2

3

4

5

6

<!-- 配置 MultipartResolver -->

  < bean id = "commonsMultipartResolver"

   class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" >

   < property name = "defaultEncoding" value = "UTF-8" ></ property >

   < property name = "maxUploadSize" value = "1024000" ></ property >

  </ bean >

然后坑就出现,费尽周折发现是id写错了,id=[multipartResolver],修改代码为:

?

1

2

3

4

5

< bean id = "multipartResolver"

  class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" >

  < property name = "defaultEncoding" value = "UTF-8" ></ property >

  < property name = "maxUploadSize" value = "1024000" ></ property >

</ bean >

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_40406929/article/details/114733221

查看更多关于SpringMVC 接收前端传递的参数四种方式小结的详细内容...

  阅读:21次