2013-08-21 33 views
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); 
} 
} 

他找到了正確的路線,但發現了瞬發異常

+0

調用'newInstance()'似乎在這裏失敗。你的路由器是否有無參數的公共構造函數? – Kapep

+1

問題是@kapep提到的問題。 Routes類沒有默認的構造函數。它有一堆靜態方法。有一個名爲'handlerFor'的程序看起來很有希望,但它需要一個'play.api.mvc.RequestHeader'對象,而不是傳遞給'onRouteRequest'方法的Java版本'play.mvc.Http.RequestHeader'。 – estmatic

+0

事實上,我以靜態方式反映路線,但我發現這些問題。那麼替代方案呢? – user2054758

回答

1

在這個框架中,對於scala而言,它有點沮喪。幾天後,我決定在每次啓動服務器時執行自動處理在主路徑文件中寫入新內容。

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 

import play.Application; 
import play.GlobalSettings; 

public class Global extends GlobalSettings 
{ 
@Override 
public void onStart(Application app) 
{ 
    String newline = System.getProperty("line.separator"); 
    File route = app.getFile("/conf/routes"); 
    File[] modules = app.getFile("/modules").listFiles(); 

    String newContents = "# start of autogenerated code" + newline; 
    for (File module : modules) 
    { 
     String moduleLow = module.getName().toLowerCase(); 
     newContents += "-> /module " + moduleLow + ".Routes " + newline; 
    } 
    newContents += "# end of autogenerated code" + newline; 
    editRoute(route, newContents, newline); 

} 

private void editRoute(File route, String newContents, String newline) 
{ 
    try 
    { 
     FileReader f = new FileReader(route); 
     BufferedReader br = new BufferedReader(f); 
     String contents = ""; 
     while (true) 
     { 
      String s = br.readLine(); 
      if (s == null) 
       break; 
      contents += s + newline; 
     } 
     br.close(); 

     FileWriter w = new FileWriter(route); 
     BufferedWriter b = new BufferedWriter(w); 
     b.write(newContents + contents); 
     b.flush(); 
     b.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onStop(Application app) 
{ 
    String newline = System.getProperty("line.separator"); 
    File route = app.getFile("/conf/routes"); 
    try 
    { 
     FileReader f = new FileReader(route); 
     BufferedReader br = new BufferedReader(f); 
     String contents = ""; 
     boolean startAutoCode = false; 
     boolean endAutoCode = false; 
     while (true) 
     { 
      String s = br.readLine(); 
      if (s == null) 
       break; 
      if (s.contains("# start of autogenerated code")) 
      { 
       startAutoCode = true; 
      } 
      else if (s.contains("# end of autogenerated code")) 
      { 
       endAutoCode = true; 
       continue; 
      } 

      if (!startAutoCode || endAutoCode) 
      { 
       contents += s + newline; 
      } 
     } 
     br.close(); 

     FileWriter w = new FileWriter(route); 
     BufferedWriter b = new BufferedWriter(w); 
     b.write(contents); 
     b.flush(); 
     b.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 
相關問題