【问题标题】:How to get query string parameters in java play framework?java play框架中如何获取查询字符串参数?
【发布时间】:2013-04-09 16:59:34
【问题描述】:

我对 java play 框架很陌生。我已经设置了所有正常路线,例如 /something/:somthingValue 和所有其他路线。现在我想创建路由接受查询参数,如

/something?x=10&y=20&z=30

这里我想获取“?”之后的所有参数作为键==>值对。

【问题讨论】:

    标签: java playframework playframework-2.0 request.querystring


    【解决方案1】:

    您可以将查询参数连接到路由文件中:

    http://www.playframework.com/documentation/2.0.4/JavaRouting 在“具有默认值的参数”部分中

    或者你可以在你的 Action 中请求它们:

    public class Application extends Controller {
    
        public static Result index() {
            final Set<Map.Entry<String,String[]>> entries = request().queryString().entrySet();
            for (Map.Entry<String,String[]> entry : entries) {
                final String key = entry.getKey();
                final String value = Arrays.toString(entry.getValue());
                Logger.debug(key + " " + value);
            }
            Logger.debug(request().getQueryString("a"));
            Logger.debug(request().getQueryString("b"));
            Logger.debug(request().getQueryString("c"));
            return ok(index.render("Your new application is ready."));
        }
    }
    

    例如,http://localhost:9000/?a=1&amp;b=2&amp;c=3&amp;c=4 在控制台上打印:

    [debug] application - a [1]
    [debug] application - b [2]
    [debug] application - c [3, 4]
    [debug] application - 1
    [debug] application - 2
    [debug] application - 3
    

    请注意,c 在 url 中是两次。

    【讨论】:

    • 我看不到 request() 的 getQueryString() 方法。是因为我用的是 play 2.0 而你用的是 2.0.4 吗?
    • 你说得对,playframework.com/documentation/api/2.0/java/play/mvc/… 不包含 getQueryString() 但 request().queryString() 可以满足你的所有需求。
    • 非常感谢。你能给我推荐一些好的教程吗?除了它的文档。
    • 太好了,那么请将我的回答标记为已接受。可在groups.google.com/forum/?fromgroups=#!topic/play-framework/… 上找到 Play 教程列表
    • 还有一个问题,因为 play 2.0 没有 getQueryString() 方法,并且 Arrays.toString(entry.getValue()) 返回带有 [] 的值,如果我不想要那些 [] 怎么办?我只想要使用 getQueryString() 方法获得的价值。我必须切换到最新版本的 Play 吗?
    【解决方案2】:

    在Play 2.5.x中,直接在conf/routes中制作,可以放默认值:

    # Pagination links, like /clients?page=3
    GET   /clients              controllers.Clients.list(page: Int ?= 1)
    

    在你的情况下(使用字符串时)

    GET   /something            controllers.Somethings.show(x ?= "0", y ?= "0", z ?= "0")
    

    使用强类型时:

    GET   /something            controllers.Somethings.show(x: Int ?= 0, y: Int ?= 0, z: Int ?= 0)
    

    有关详细说明,请参阅:https://www.playframework.com/documentation/2.5.x/JavaRouting#Parameters-with-default-values

    【讨论】:

      【解决方案3】:

      您可以将所有查询字符串参数作为 Map:

      Controller.request().queryString()
      

      此方法返回一个Map&lt;String, String[]&gt; 对象。

      【讨论】:

        【解决方案4】:

        Java/Play 1.x 中,您可以使用:

            Request request = Request.current();
            String arg1 = request.params.get("arg1");
        
            if (arg1 != null) {
                System.out.println("-----> arg1: " + arg1);
            } 
        

        【讨论】:

          【解决方案5】:

          你可以使用FormFactory:

          DynamicForm requestData = formFactory.form().bindFromRequest();
          String firstname = requestData.get("firstname");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-05-16
            • 2022-06-24
            • 2017-12-09
            • 1970-01-01
            • 2012-03-28
            • 1970-01-01
            相关资源
            最近更新 更多