Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

playframework 2.0 - Request.params is gone in Play Framework 2.0

Is there a way to access all request parameters, regardless of HTTP method? I have poured over the documentation and the api without finding a way in Play Framework 2.0.

I have a search on a site that accepts POST and GET. The custom tracking on the site examines all parameters passed in to determine the correct way to store tracking data.

In the Play Framework 1.2.x, I was able to access parameters from a request in a Controller with

request.params.get("keywords")
request.params.get("location") 
request.params.all()

With Play Framework 2.0, this is no longer the case. The Request no longer has the method params, only queryString and queryString only works with GET and not POST.

It is not feasible to define every single possible tracking parameter into the Controller Action, they are dynamic.


UPDATE: A possible work around is using Body Parsers.

Depending on the content type of the request, the appropriate parser is used, e.g. application/form-url-encoded vs application/json

This is the crude Map that combines POST parameters and GET parameters, with GET parameters taking precedence.

val params: collection.mutable.Map[String, Seq[String]] = collection.mutable.Map() 
params ++= request.body.asFormUrlEncoded.getOrElse[Map[String, Seq[String]]] { Map.empty } 
params ++= request.queryString 
question from:https://stackoverflow.com/questions/9808348/request-params-is-gone-in-play-framework-2-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Play 2.0 documentation doesn't explain this very well. Body parsers are an acceptable solution, but I've found DynamicForm to be much more lightweight and user friendly.

The documentation can be found here.

In particular, the DynamicForm.bindFromRequest() is a good place to start if you are trying to replace the old Play 1.0 request.params.get().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...