【问题标题】:java play framework 2.1.3 How retrive a routes objectjava play framework 2.1.3 如何获取一个路由对象
【发布时间】:2013-08-21 19:14:16
【问题描述】:

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

-> /mymodule mymodule.Routes

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

我尝试在 onRouteRequest() 中编写如下代码

Class.forName("mymodule.Routes").routes.lift(request);

但是失败了,有什么建议吗?

编辑 1:在游戏 1 中可能是这样的:

/{controller}/{action} {controller}.{action}

但在 play2 中似乎也不起作用

edit 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 提到的。 Routes 类没有默认构造函数。它有一堆静态方法。有一个名为handlerFor 的方法看起来很有希望,但它需要一个play.api.mvc.RequestHeader 对象,而不是传递给onRouteRequest 方法的Java 版本play.mvc.Http.RequestHeader
  • 事实上我以静态方式反映了路由,但我发现了这些问题。那么替代解决方案呢?
  • 您能否创建一个测试项目,这样我们就不必复制您的所有工作,而是尝试找出您的设置出了什么问题。将其上传到 github 或其他任何地方,并在此处分享链接。我们可以通过这种方式提供帮助的可能性要高得多。
  • github.com/jstar88/PlayTests 这里有代码,但你不能用 java 反映框架,因为框架是用 scala 构建的

标签: playframework-2.0


【解决方案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();
    }
}
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多