[英]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}
考試:
http://localhost:9000/data
調用getData
http://localhost:9000/data?name=test
調用getData
結果:
我想要的結果是什么:
我很感激你的幫助。 謝謝...
以下是基於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
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.