This is partly related to
https://groups.google.com/forum/?fromgroups#!topic/google-sitebricks/KfKWJM6SUPo because I have the need to serve mutiple @Ats with the same controller class
My use case:
I am developing a CMS, where a user can add a page and assign as slug to it. The slug can have 1 to n levels, and will be relative to the root
etc...
Ideally I would want to use multiple @At annotations in the same controller class, and with a catch-all wildcard that handles the request if nothing else matches, but from what I have understood from the other thread (please correct me if I am wrong) this is simply not possible. I would argue that it would still be logically possible to do this in a way that would produce the expected outcome (without knowing the internals of the Sitebricks framework) as long as the @At value is matched where the fixed values have prio over the wildcards, and parsing this level by level.
In the perfect world, @At("/:slug") would catch "//
www.mycms.com/test/test2/" with "/test/test2" as the identified slug (as long as no other @At notations suits better). As/if this is not possible with the current version, I would like to request it as a future feature.
However, for now I have sort of settled with an ugly alternative, where I limit the number of levels a slug can have to a certain number (not decided exactly how many yet), and attempting to implement it like this:
PageService.class
public interface PageService {
PageService setSlug(String slug);
Reply<?> getReply();
String getTitle();
String getContent();
}
PageServiceImpl.class
@Show("/front/page.html")
public class PageServiceImpl implements PageService {
@Inject
private PageDao pageDao;
protected String slug;
private Page page;
private Reply<?> reply;
public PageServiceImpl() {
}
@Override
public PageServiceImpl setSlug(String slug) {
this.slug = slug;
// Simplified logic (skipping error handling, response code management etc):
this.page = this.pageDao.getBySlug(slug);
this.reply = Reply.with(this).saying().ok()
.template(PageServiceImpl.class)
.as(Text.class)
.type("text/html");
LOGGER.info(String.format("Page for slug '%s' found: '%s'",
return this;
}
@Override
public Reply<?> getReply() {
return this.reply;
}
@Override
public String getTitle() {
return page.getDocument().getTitle();
}
@Override
public String getContent() {
return page.getDocument().getContent();
}
}
PageLevelOneService.class
@At("/:slug/")
@Service
public class PageLevelOneService {
@Inject
private Injector injector;
@Get
public Reply<?> service(@Named("slug") String slug) {
return injector.getInstance(PageService.class).setSlug(slug).getReply();
}
}
PageLevelTwoService.class
@At("/:slug/")
@Service
public class PageLevelTwoService {
@Inject
private Injector injector;
@Get
public Reply<?> service(@Named("slug") String slug) {
return injector.getInstance(PageService.class).setSlug(slug).getReply();
}
}
/front/page.html
<html>
<body>
<h1>${title}</h1>
<div>${content}</div>
</body>
</html>
Creating a testpage with the slug "test" and trying to access it, produces the following output to the console:
Page for slug 'test' found: 'true'
But the template is not parsed correctly:
HTTP ERROR 500
Problem accessing /test/. Reason:
[Error: unresolvable property or identifier: title]
[Near : {... title ....}]
^
[Line: 1, Column: 1]
(see http://pastebin.com/H5KvDZmY for full response with stacktraces)
What am I doing wrong here, and is there a more elegant way I could solve my problem?
Thanks