好得很程序员自学网

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

使用controller传boolean形式值

controller传boolean形式值

?

1

2

3

4

@GetMapping ( "/check-cart" )

public List<CartViewDto> checkCart( @RequestParam (value = "requirePrice" , required = false ) boolean requirePrice) {

         return service.checkCart(requirePrice);

     }

controller传入boolean类型的值,参数为非必填

我们可以传三种类型

http://端口:ip/cart/check-cart http://端口:ip/cart/check-cart?requirePrice=true http://端口:ip/cart/check-cart?requirePrice=false

这三种都可以。

其他如:

http://端口:ip/cart/check-cart?requirePrice= http://端口:ip/cart/check-cart?requirePrice=111

都会报错

controller层接收各种参数和文件

在构建一个系统时,前端和后台总是需要对接,在springmvc架构里,这种对接一般发生在Controller层中。方法参数绑定首先支持Java所有基本类型(包括: byte、short、int、long、float、double、char、string、boolean),以及基本类型对应封装高级类(包括:StringBuilder、StringBuffer),也包含 了我们自己定义的各种JavaBean类型。接受的方式有很多,但是也要在接受数据时也要体现面向对象的思想。

简单传入

以用户登陆为例,在前台传入数据,做一个传入的例子。

前台传入数据的形式为 username=10&password=10

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<html>

<head>

<script src= "https://cdn.bootcss测试数据/jquery/3.3.1/jquery.js" ></script>

<script>

     //简单形式的传参

     $.ajax({

             type: "POST" ,

             url: "http://localhost:8080/test/requesetParamGet" ,

             contentType: "application/x-www-form-urlencoded" ,

             data: "username=10&password=10" ,

             dataType: "json" ,

             success: function (result) {

                 if (result.code == 0) {

                     console.log(result)

                 } else {

                   

                 }

             }

         });

</script>

</head>

</html>

在这种情况下,如何在后台进行接受呢?可以采用如下的方法。

?

1

2

3

4

5

6

    @ResponseBody

    @RequestMapping ( "/simple" )

    public R list(String name,String age){

        System.out.println( "name:" +name+ ",age:" +age);

        return R.ok();

    }

当然也可以采用HttpServeletRequest的形式进行接受

?

1

2

3

4

5

6

    @ResponseBody

    @RequestMapping ( "/simple" )

    public R requestGet(HttpServletRequest request){

        System.out.println( "reqname:" +request.getParameter( "name" )+ ",reqage:" +request.getParameter( "age" ));

        return R.ok();

    }

以json对象的形式传入

还是以用户登陆为例,在前台传入数据,做一个传入的例子。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<script src= "https://cdn.bootcss测试数据/jquery/3.3.1/jquery.js" ></script>

<script>

     //class获取,需要用对象的形式

     var s={

     name: 'liMin' ,

     age: '10'

     }

     $.ajax({

             type: "POST" ,

             url: "http://localhost:8080/test/classGet" ,

             contentType: "application/json" ,

             data:JSON.stringify(s),

             dataType: "json" ,

             success: function (result) {

                 if (result.code == 0) {

                     console.log(result)

                 } else {

                   

                 }

             }

         });

</script>

这种情况下,controller层建议使用对象的形式进行接受。

?

1

2

3

4

5

6

    @ResponseBody

    @RequestMapping ( "/classGet" )

    public R classGet( @RequestBody TestEntity testEntity){

        System.out.println( "className:" +testEntity.getName()+ "classAge:" +testEntity.getAge());

        return R.ok();

    }

在传参时,需要添加注解@RequsetBody用来接收contentType为application/json的传入对象。如果传过来的时contentType头为application/x-www-form-urlencoded,那么建议使用另外一个注解接受@RequestParam来接受。

?

1

2

3

4

5

6

    @RequestMapping ( "/requesetParamGet" )

    public R addUser6( @RequestParam ( "username" ) String username, @RequestParam ( "password" ) String password) {

        System.out.println( "username is:" +username);

        System.out.println( "password is:" +password);

        return R.ok();

    }

如果啥都不写,那么就得不到这个对象,接受到的对象为NULL。

当然,你可以不接受为一个对象,可以把传过来的json对象转化为json字符串,然后用各种工具进行解析,也是可以的。当然也是要加上@RequestBody或者@RequestParam的。

?

1

2

3

4

5

6

    @ResponseBody

    @RequestMapping ( "/stringGet" )

    public R stringGet( @RequestBody   String string){

        System.out.println( "String:" +string);

        return R.ok();

    }

文件传输

在项目中,文件上传有别于对象的上传。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<html>

<head>

<script src= "https://cdn.bootcss测试数据/jquery/3.3.1/jquery.js" ></script>

<script>

 

     function savePic(){

         var formData = new FormData($( "#uploadPic" )[0]); 

         var ajaxUrl = "http://localhost:8080/test/fileUpload" ;

         //alert(ajaxUrl);

         //$('#uploadPic').serialize() 无法序列化二进制文件,这里采用formData上传

         //需要浏览器支持:Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。

         $.ajax({

             type: "POST" ,

             //dataType: "text",

             url: ajaxUrl,

             data: formData,

             //async: false, 

             //cache: false, 

             contentType: false ,  //上传文件

             processData: false , //序列化处理,默认为true,上传文件需要改成false

             success: function (data) {

                 alert(data);

             },

             error: function (data) {

                 alert( "error:" +data.responseText);

 

              }

         });

         return false ;

     }

     function jiance(){

    

         var formData = new FormData();

         formData.append()      

         }

</script>

</head>

<body>

     <form id= "uploadPic" action= "" enctype= "multipart/form-data" >

         <input type= "file" name= "multipartFile" id= "file" >

         <a href= "javascript:savePic();" class= "btn green" > 提交 </a>

         <a href= "javascript:jiance();" class= "btn green" > jiance </a>

     </form>

</body>

</html>

在后台接受参数的例子:

?

1

2

3

4

5

6

7

8

9

    @RequestMapping ( "/fileUpload" )

    @ResponseBody

    public R upload(MultipartFile multipartFile){

        String filePath= "" ;

        if (!multipartFile.isEmpty()){

            System.out.println(multipartFile.getOriginalFilename());

        }

        return R.ok().put( "filePath" ,filePath);

    }

?

1

2

3

4

5

6

7

8

9

    @RequestMapping ( "/fileUpload2" )

    @ResponseBody

    public R upload2( @RequestParam ( "multipartFile" ) MultipartFile multipartFile){

        String filePath= "" ;

        if (!multipartFile.isEmpty()){

            System.out.println(multipartFile.getOriginalFilename());

        }

        return R.ok().put( "filePath" ,filePath);

    }

?

1

2

3

4

5

6

7

8

9

    @RequestMapping ( "/fileUpload3" )

    @ResponseBody

    public R upload3( @RequestBody MultipartFile multipartFile){

        String filePath= "" ;

        if (!multipartFile.isEmpty()){

            System.out.println(multipartFile.getOriginalFilename());

        }

        return R.ok().put( "filePath" ,filePath);

    }

这里需要注意一点,文件名必须和参数名保持一致,在本项目中file文件的名字必须为multipartFile。

原文链接:https://blog.csdn.net/starkpan/article/details/102817818

查看更多关于使用controller传boolean形式值的详细内容...

  阅读:29次