2013-08-21 8 views
5

動的モジュールルーティングを作成しようとしているため、glob.javaクラスの文字列からルートオブジェクトを取得する方法を知りたいと思います。私 主要なルートを編集するために、私はJavaプレイフレームワーク2.1.3ルートオブジェクトの取得方法

としてモジュール(サブプロジェクト)を追加するたびに - > /のmymodule mymodule.Routes

だからこれを避けるために、私はに依存し、目標ルートをロードするためにトリングよURIパス。 /モジュール/のmymodule

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.vm.RequestHeader'オブジェクトは 'onRouteRequest'メソッドに渡されるJavaバージョンの' play.mvc.Http.RequestHeader'ではなく、 'play.api.mvc.RequestHeader'オブジェクトを取ります。 – estmatic

+0

infact私は静的な方法でルートを反映しましたが、私はこれらの問題を発見しました。代替のソリューションですか? – user2054758

答えて

1

このフレームワークでは、スカラーに対するJava劣等感が少し崩れています。数日後、私はサーバーが起動されるたびにメインルートファイルに新しい内容を書き込む自動処理を実行することに決めました。

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(); 
    } 
} 
} 
関連する問題