如何在Play Project上的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}
测试:
getData
http://localhost:9000/data
getData
http://localhost:9000/data?name=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
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"
答案 0 :(得分:2)
我认为不可能像你想要的那样在路径文件中重载它。
但你可以这样做:
public static void getData(String name) {
if (name == null) {
renderText("Without Parameter");
} else {
renderText("With Parameter name = " + name);
}
}
答案 1 :(得分:1)
我相信我想要实现和你一样的事,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;
}
它不是很漂亮,但从服务客户端的角度来看,它可能比使用两个单独的路径更好。