gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:43:47 25 4
gpt4 key购买 nike

我想知道如何从 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

最佳答案

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

关于playframework-2.0 - java play framework 2.1.3 如何检索路由对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18365766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com