How to set overload method in Application.java in Play Project?
Here is an example of what I'm doing right now:
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);
}
}
<strong> routes
GET / Application.index
GET /data Application.getData
GET /favicon.ico 404
GET /public/ staticDir:public
* /{controller}/{action} {controller}.{action}
Test:
- Call
getDatawithout a parameterhttp://localhost:9000/data - Call
getDatawith parameterhttp://localhost:9000/data?name=test
Result:
- With parameter name = null
- With parameter name = test
What I want for the result:
- No parameter
- With parameter name = test
I am very grateful to you for your help. Thank...
Update Solution
Here's what I do based on Daniel Alexiuc's suggestion :
Application.java
public class Application extends Controller {
public static void index() {
render();
}
public static void getData() {
getDataName(null);
}
public static void getDataName(String name) {
if(name == null)
renderText("Without Parameter");
else
renderText("With Parameter name = " + name);
}
}
<strong> routes
GET / Application.index
GET /default Application.getData
GET /dataString Application.getDataName
Solution Update (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);
}
}
}
<strong> routes
GET /data Application.getData
GET /data/{name} Application.getData
GET /data/{name}/{age} Application.getData
And to call:
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"
source
share