簡體   English   中英

Application.java Play Framework中的重載方法

[英]Overload method in Application.java Play Framework

如何在Play項目中的Application.java中設置重載方法?

這是我正在做的一些例子:

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        renderText("Without Parameter");
    }

    public static void getData(String name) {
        renderText("With Parameter name = " + name);
    }
}

路線

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                                       Application.index
GET     /data                                   Application.getData

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}

考試:

  1. 不帶參數http://localhost:9000/data調用getData
  2. 使用參數http://localhost:9000/data?name=test調用getData

結果:

  1. 使用參數名稱= null
  2. 使用參數名稱= test

我想要的結果是什么:

  1. 沒有參數
  2. 使用參數名稱= test

我很感激你的幫助。 謝謝...


更新方案

以下是基於Daniel Alexiuc建議我正在做的事情:

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        /** 
         *  Do some process before call getDataName method 
         *  and send the result of the process as the parameter.
         *  That's why I do this way that look like redundancy process.
        **/
        getDataName(null);
    }

    public static void getDataName(String name) {
        // Didn't use ternary operation here because will become error 'not a statement'
        if(name == null)
            renderText("Without Parameter");
        else
            renderText("With Parameter name = " + name);
    }

}

路線

GET     /                                       Application.index
GET     /default                                Application.getData
GET     /dataString                             Application.getDataName

更新解決方案(26/07)

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData(String name, Integer age) {
        if (name == null && age == null) {
            renderText("Data null");
        } else if (name != null && age == null) {
            renderText("Name: " + name);
        } else if (name != null && age != null) {
            renderText("Name: " + name + "\n" + "Age: " + age);
        }
    }
}

路線

GET     /data                                   Application.getData
GET     /data/{name}                            Application.getData
GET     /data/{name}/{age}                      Application.getData

而對於電話:

 1. http://localhost:9000/data                -> Display "Data null"
 2. http://localhost:9000/data/Crazenezz      -> Display "Name: Crazenezz"
 3. http://localhost:9000/data/Crazenezz/24   -> Display "Name: Crazenezz
                                                          Age: 24"

我不認為可以像你想要的那樣在路徑文件中重載它。

但你可以這樣做:

public static void getData(String name) {
  if (name == null) {
    renderText("Without Parameter");
  } else {
    renderText("With Parameter name = " + name);
  }
}

我相信我想要實現與你一樣的事情,Crazenezz,所以首先我嘗試使用以下路線定義:

GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:Long ?= null)

...與控制器中的方法一起檢查參數是否為null,但由於null不是路由定義中的有效可選值而失敗。 所以我相信我會使用以下解決方案(到目前為止工作),在JSON服務接口中公開一個字符串值,我在控制器中將其轉換為long。

GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:String ?= "")


@Transactional(readOnly=true)
public static Result findTermsByQuery(String languageId) {
    Result result;
    if (languageId == "")
        result = ok(toJson(TermService.getTerms()));
    else
        result = ok(toJson(TermService.findTermsByLanguageId(Long.parseLong(languageId.trim()))));
    return result;
}

它不是很漂亮,但從服務客戶端的角度來看,它可能比使用兩個單獨的路徑更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM