Как установить метод перегрузки в Application.java в Play Project?
Вот пример того, что я сейчас делаю:
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}
Тест:
getData
без параметраhttp://localhost:9000/data
getData
с параметромhttp://localhost:9000/data?name=test
Результат:
Что я хочу от результата:
Я очень ценю вашу помощь. Спасибо...
Вот что я делаю на основе Даниэля Алексюка предложения:
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"