Play framework 2: Use Array[String] in route

后端 未结 4 693
忘了有多久
忘了有多久 2021-02-05 13:56

I want to generate an url like this:

/photo?tags=tag1,tag2,tag3

routes file:

GET     /photo  controllers.Photos.list         


        
相关标签:
4条回答
  • 2021-02-05 14:02

    I think that you should use a common String and then take care about converting it to an Array in your controller

    routes:

    GET     /photo  controllers.Photos.list(tags:String ?= "")
    

    in Java:

    public static Result list (String tags){
        String[] tagsArray = tags.split(",");
        // do something with tagsArray
        return ok();
    }
    
    0 讨论(0)
  • 2021-02-05 14:12

    As an aside, if you wish to pass an array of Longs, this works:

    GET /photo controllers.Photos.list(tags: java.util.List[java.lang.Long])

    with the controller function taking List<Long> tags as an argument.

    0 讨论(0)
  • 2021-02-05 14:27

    play will bind to array's/lists when the values are in the query string or post data with the same name.

    this also seems to work:

    This route: http://localhost/controller/{id} 
    

    This url: http://localhost/controller/1?id=2&id=3

    Will bind to controller(int[] id) where id -> {1, 2, 3}

    posting id=2&id=3 will also bind to an array.

    reference: https://groups.google.com/forum/?fromgroups#!topic/play-framework/c5kB6wmcF8Q

    0 讨论(0)
  • 2021-02-05 14:28

    Using a list instead of an array should work.

    If you are using Java, it works like this:

    GET     /photo  controllers.Photos.list(tags: java.util.List[String])
    
    0 讨论(0)
提交回复
热议问题