5
我想知道如何從global.java類中的字符串中檢索路由對象,因爲我試圖做一個動態模塊路由:我不想編輯我 主要航線,每次我添加一個模塊(子項目)作爲java play framework 2.1.3如何檢索路由對象
- >/mymodule中mymodule.Routes
因此,爲了避免這種情況,我特林目標路徑依賴的加載URI路徑。 /模塊/ mymodule中
我tryed寫一些代碼,遵循內部onRouteRequest()
Class.forName("mymodule.Routes").routes.lift(request);
,但失敗了,有什麼建議?
編輯1:在播放1是可能的類似的東西:
/{controller}/{action} {controller}.{action}
但play2似乎不工作以及
編輯2:我目前Global.java是
import play.GlobalSettings;
import play.Play;
import play.api.mvc.Handler;
import play.mvc.Http;
public class Global extends GlobalSettings
{
@Override
public Handler onRouteRequest(Http.RequestHeader request)
{
String path = request.path();
if (path.startsWith("/module/"))
{
String[] paths = path.split("/");
String router = paths[2];
try
{
return (Handler) Class.forName(router + ".Routes", true, Play.application().classloader()).newInstance();
}
catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
return super.onRouteRequest(request);
}
}
他找到了正確的路線,但發現了瞬發異常
調用'newInstance()'似乎在這裏失敗。你的路由器是否有無參數的公共構造函數? – Kapep
問題是@kapep提到的問題。 Routes類沒有默認的構造函數。它有一堆靜態方法。有一個名爲'handlerFor'的程序看起來很有希望,但它需要一個'play.api.mvc.RequestHeader'對象,而不是傳遞給'onRouteRequest'方法的Java版本'play.mvc.Http.RequestHeader'。 – estmatic
事實上,我以靜態方式反映路線,但我發現這些問題。那麼替代方案呢? – user2054758