java play framework 2.1.3如何检索路由对象

时间:2013-08-21 19:14:16

标签: playframework-2.0

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

- > / mymodule mymodule.Routes

为了避免这种情况,我要根据URI路径加载目标路由。 / module / 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);
}
}

他找到了正确的路线,但抛出了瞬间异常

1 个答案:

答案 0 :(得分:1)

在这个框架中,它对scala的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();
    }
}
 }