5

我想知道如何从 global.java 类中的字符串中检索路由对象,因为我试图进行动态模块路由:我不想在每次添加模块时编辑我的主路由(子项目)作为

-> /mymodule mymodule.Routes

所以为了避免这种情况,我试图根据 URI 路径加载目标路由。/模块/我的模块

我尝试在 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);
}
}

他找到了正确的路线,但引发了 Instantation Exception

4

1 回答 1

1

在这个框架中,Java 对 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();
    }
}
 }
于 2013-08-24T13:34:39.903 回答