2013-04-09 14 views

risposta

32

È possibile cablare nei tuoi parametri di query nel file rotte:

http://www.playframework.com/documentation/2.0.4/JavaRouting nella sezione "I parametri con valori di default"

Oppure si può chiedere per loro nel vostro Azione:

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.")); 
    } 
} 

Ad esempio le stampe http://localhost:9000/?a=1&b=2&c=3&c=4 sulla console:

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

Nota che c è due volte nell'URL.

+0

io non sono in grado di vedere il metodo getQueryString() di richiesta(). è perché sto usando il gioco 2.0 e stai usando 2.0.4? – Sadik

+0

Hai ragione, http://www.playframework.com/documentation/api/2.0/java/play/mvc/Http.Request.html non contiene getQueryString() ma request(). QueryString() può darti tutti hai bisogno. – Schleichardt

+0

Grazie mille amico. mi puoi suggerire dei buoni tutorial su di esso? Tranne la sua documentazione. – Sadik

7

È possibile ottenere tutti i parametri di stringa di query come una mappa:

Controller.request().queryString() 

Questo metodo restituisce un oggetto Map<String, String[]>.

0

In Java/Play 1.x li ottiene con:

Request request = Request.current(); 
    String arg1 = request.params.get("arg1"); 

    if (arg1 != null) { 
     System.out.println("-----> arg1: " + arg1); 
    } 
9

In Play 2.5.x, è realizzato direttamente in conf/routes, dove si possono mettere i valori di default:

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

Nel tuo caso (quando utilizzando stringhe)

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

Quando si utilizza tipizzazione forte:

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

Vedere: https://www.playframework.com/documentation/2.5.x/JavaRouting#Parameters-with-default-values per una spiegazione più lunga.

0

È possibile utilizzare FormFactory:

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