【发布时间】:2015-05-17 14:01:59
【问题描述】:
这是我正在尝试做的基本操作。我正在从事一个项目,我正在从各种 Web API 创建一个混搭页面。混搭页面包含国家/地区信息。
我有一个 BLL 层,我在其中创建混搭页面(使用块库输入国家/地区的 html 页面)。这是用来设置模板的方法
public String GetTemplate(Country country) throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("BLL/themes/page.html");
Theme theme = new Theme("src/BLL", "themes");
Chunk html = theme.makeChunk("page", "html");
html.set("countryName", country.getName());
html.set("countryCode", country.getCode());
html.set("countryContinent", country.getContinent());
html.set("countryCapital", country.getCapital());
html.set("countryAreaInSqKm", country.getAreaInSqKm());
html.set("countryPopulation", country.getPopulation());
html.set("countryCurrencyCode", country.getCurrencyCode());
html.set("countryExchangeRate", country.getExchangeRate());
html.set("countryFlagUrl", country.getFlagUrl());
html.set("countryDescription", country.getDescription());
html.set("countryMap", country.getMapLocation());
return html.toString();
}
最终,我将使用所有可用国家/地区的数据库。
所以我想在 Restlet 服务器上做的是遍历所有国家,像这样,还是以类似的方式?
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/countries/", Countries.class);
for (Country country : cm.GetAllCountries()) {
router.attach("/countries/" + country.getName(), new CountryTemplate(country));
// Obviously this doesn't work, and it doesn't work with getClass() either.
}
return router;
}
因此,我试图让我的资源在 get 方法中返回 html(来自 TemplateManager):
public class CountryTemplate extends ServerResource {
CountryManager cm = null;
TemplateManager tm = null;
Country country = null;
public CountryTemplate(Country _country) throws Exception {
cm = new CountryManager();
tm = new TemplateManager();
country = _country;
}
@Get("html")
public String getTemplate() throws IOException, RemoteException, UnsupportedCurrencyException {
return tm.GetTemplate(country);
}
}
确实有办法做到这一点,还是我接近这一切都错了?
如果其他人发现自己在这里,Tim 的代码完全符合要求。发布的代码已更改为他的版本。
【问题讨论】:
-
@TimBiegeleisen 你能指点一下吗? :)
-
如果您对我的回答有任何疑问,请告诉我。如果您愿意,我们可以进行聊天。
-
这正是我所需要的,谢谢!我想我将其标记为已解决。
标签: java resources restlet mashup