Closed bselack closed 9 months ago
Hi @bselack,
I need a bit more information. What version of Sitemesh are you using? Are you using Spring Boot? Are you deploying a war or a jar? Have you tried setting the decorator prefix in sitemesh3.xml?
Also what version of java
@codeconsole
sitemesh-3.2.0-M2.jar Java 11 No Spring Boot just Javalin (https://javalin.io/) Currently running in Eclipse, but eventually will just be a .jar Doing configurations via java, since I have no access to the embedded Jetty for placing files.
Brought sitemesh into Javalin like this.. (and I see in logging, that it does load)
var app = Javalin.create(config -> {
config.staticFiles.add(staticFiles -> {
staticFiles.hostedPath = "/"; // change to host files on a subpath, like '/assets'
staticFiles.directory = "/"; // the directory where your files are located
staticFiles.location = Location.CLASSPATH; // Location.CLASSPATH (jar) or Location.EXTERNAL (file system)
staticFiles.precompress = false; // if the files should be pre-compressed and cached in memory (optimization)
staticFiles.aliasCheck = null; // you can configure this to enable symlinks (= ContextHandler.ApproveAliases())
});
config.jetty.contextHandlerConfig(sch -> {
sch.addFilter(MyConfigurableSiteMeshFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
});
}).start(1234);
I guess my thoughts here were if I could get Sitemesh to look at the resource directories in the Javalin app, and not in Jetty's resources.
Sorry, I have never used Javalin before and I am trying to make up an example. What package is the Location
class in?
Location.CLASSPATH
Where is your code going to serve the resources from? src/main/resources
?
Not sure on LOCATION, as most of the GitHub files are Kotlin https://github.com/search?q=repo%3Ajavalin%2Fjavalin%20Location.CLASSPATH&type=code
And yes, I'd be serving from /src/main/resources
I do have a small eclipse project if that would help...
I found the import import io.javalin.http.staticfiles.Location
I tried your cade above and I am getting a 404 if I put a file in the src/main/resources
directory.
Right now, I am just trying to figure out how to get javalin to serve a resource
Thanks. From what I read, the decorators should not go where the static files are, because then they can be accessed directly. But you likely know if that would be an issue or not. I appreciate you help.
diver-demo.zip Here's a project you can work with. Let me know if you have any issues. I can delete the file once you have it.
Hi @bselack, I'll take a look at it later today
So this is how you would configure it:
config.jetty.contextHandlerConfig(sch -> {
FilterHolder sitemeshFilter = new FilterHolder(new ConfigurableSiteMeshFilter() {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.setDecoratorPrefix("/decorators/");
builder.addDecoratorPath("/*", "default.html");
}
}
);
sch.addFilter(sitemeshFilter, "/*", EnumSet.of(DispatcherType.REQUEST));
});
This would assume you put all your decorators in a /decorators/
folder with your other static files.
However, I am seeing a few issues with how it attempts to serve content as plain/text initially and then also how it is returning requests with status 304.
Yea, that's pretty much what I did with my MyConfigurableSiteMeshFilter, but didn't seem to help. I'm not sure that this will work since the files aren't visible to Jetty, and if Javalin server then from static, the Freemarker tags will likely not work. I've been trying to figure out how to get Jetty to have a alias/symlink to a directory outside of Jetty and in my app.
I appreciate your help on this, but if you think it's just a big rabbit hole (as I'm starting to think), I don't want you wasting your time on it. Just let me know if you have any other thoughts on it.
Are you able to serve freemarker views on javalin without SiteMesh?
yes... my sample does that part.
Hi @bselack,
I was able to get it to work. It works fine if you decorate the freemarker pages. The only issue is if you try to decorate a static page because of the behavior I mentioned previously that only applies to serving static resources.
@codeconsole Great! Do you have an example configuration I can use? I'm not concerned about the static, there will be no static pages, just static resources (e.g. CSS)
Sure, here you go:
public static void main(String[] args) {
JavalinFreemarker.init(configureFreemarker("/templates"));
var app = Javalin.create(config -> {
config.staticFiles.add(staticFiles -> {
staticFiles.hostedPath = "/static";
staticFiles.directory = "/";
staticFiles.location = Location.CLASSPATH;
});
config.jetty.contextHandlerConfig(sch -> {
sch.addFilter(new FilterHolder(new ConfigurableSiteMeshFilter() {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.setMimeTypes("text/html", "text/plain")
.setDecoratorPrefix("")
.addDecoratorPath("/*", "/decorators/default")
.addExcludedPath("/static/*")
.addExcludedPath("/decorators/*");
}
}), "/*", EnumSet.of(DispatcherType.REQUEST));
});
})
.get("/", ctx -> ctx.render("/hello.ftl", Collections.singletonMap("user", "Bob")))
.get("/decorators/default", ctx -> ctx.render("/decorators/default.ftl"))
.start(7070);
}
Note part of getting it to work required adding the "text/plain"
due to the behavior of that getting set for some reason by Javalin during the rendering process.
Thanks so much! Do you have a tip jar/buy me a coffee?
lol, how do I set that up?
When/if you do... let me know. Good work is appreciated and should be rewarded. Best Wishes. Barrie
ok, thanks Barrie. Feel free to reach out anytime if you have any more questions.
Hi @bselack, we should have an official release shortly.
Which will allow a bit cleaner configuration as well:
public static void main(String[] args) {
JavalinFreemarker.init(configureFreemarker("/templates"));
var app = Javalin.create(config -> {
config.staticFiles.add(staticFiles -> {
staticFiles.hostedPath = "/static";
staticFiles.directory = "/";
staticFiles.location = Location.CLASSPATH;
});
config.jetty.contextHandlerConfig(sch -> {
sch.addFilter(new FilterHolder(ConfigurableSiteMeshFilter.create(b ->
b.setMimeTypes("text/html", "text/plain")
.setDecoratorPrefix("")
.addDecoratorPath("/*", "/decorators/default")
.addExcludedPath("/static/*")
.addExcludedPath("/decorators/*")
)), "/*", EnumSet.of(DispatcherType.REQUEST));
});
})
.get("/", ctx -> ctx.render("/hello.ftl", Collections.singletonMap("user", "Bob")))
.get("/decorators/default", ctx -> ctx.render("/decorators/default.ftl"))
.start(7070);
}
This closure config won't work until the release is out, so don't try it until then.
BTW, if you still feel like sending over that coffee , you can do it here ;) https://github.com/sponsors/codeconsole
Scott,
Thanks. Am traveling this week and having LOTS of meetings, so sorry I haven't responded sooner. That coffee/reward is on the way :-)
Barrie
On Mon, Jan 22, 2024 at 7:44 PM Scott Murphy @.***> wrote:
Hi @bselack https://github.com/bselack, we should have an official release shortly.
Which will allow a bit cleaner configuration as well:
public static void main(String[] args) { JavalinFreemarker.init(configureFreemarker("/templates")); var app = Javalin.create(config -> { config.staticFiles.add(staticFiles -> { staticFiles.hostedPath = "/static"; staticFiles.directory = "/"; staticFiles.location = Location.CLASSPATH; }); config.jetty.contextHandlerConfig(sch -> { sch.addFilter(new FilterHolder(ConfigurableSiteMeshFilter.create(b -> b.setMimeTypes("text/html", "text/plain") .setDecoratorPrefix("") .addDecoratorPath("/*", "/decorators/default") .addExcludedPath("/static/*") .addExcludedPath("/decorators/*") )), "/*", EnumSet.of(DispatcherType.REQUEST)); }); }) .get("/", ctx -> ctx.render("/hello.ftl", Collections.singletonMap("user", "Bob"))) .get("/decorators/default", ctx -> ctx.render("/decorators/default.ftl")) .start(7070); }
This closure config won't work until the release is out, so don't try it until then.
BTW, if you still feel like sending over that coffee , you can do it here ;) https://github.com/sponsors/codeconsole
— Reply to this email directly, view it on GitHub https://github.com/sitemesh/sitemesh3/issues/138#issuecomment-1905091298, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEI2PYFCGETQMUHUMA5T7V3YP4BYRAVCNFSM6AAAAABCAVU4TSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBVGA4TCMRZHA . You are receiving this because you were mentioned.Message ID: @.***>
@bselack Hi Barrie,
Thanks for the coffees. Were much enjoyed. :)
I just did a release with a javalin example.
Thanks! I'll check it out later this week. Quite busy lately :-)
I've configured Sitemesh 3 in Javalin and it seems to be loading fine as a servlet filter, but because it's a application with an embedded Jetty, I don't have access to the WEB-INF directory for my decorators (also using Freemarker) and I've tried various settings and location in my eclipse project, but they are never found. Is there a way to specify a directory in my application classpath?