[英]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.