Reputation: 21
I tried Search the documentation but I still don't understand. Here's my code to start up the app.
public static void main(String[] args){
Javalin app = Javalin.create(
config -> {
config.addStaticFiles("src/main/resources/public", Location.EXTERNAL);
}
);
app.start(9090);
app.get("/", ctx -> {
ctx.redirect("login.html");
});
}
on localhost:9090/login.html, The Login Page Displays The brackets.
{% extends "base.html" %} {% block title %} Login {% endblock %} {% block content %}
{{success}}
Login In
Username
password
{% endblock %}
Upvotes: 2
Views: 1058
Reputation: 21993
Overview
In your Javalin route you are perforing a redirect:
ctx.redirect("login.html");
Instead of redirecting to a template, you should redirect to a path - and then handle the rendering of the template in that path.
Also, to avoid needing to explicitly configure a Pebble rendering engine (in other words, to use Pebble as the default renderer) you can change your pebble template file suffixes from .html
to .peb
.
(If you use .html
then Javalin will expect you to be using Thymeleaf, not Pebble.)
The Template Files
Both of the Pebble template files need to be placed in the standard resources
folder under src
, with a suitable file extension. As per the documentation:
Javalin looks for templates/markdown files in src/resources, and uses the correct rendering engine based on the extension of your template.
Do not place your templates in the public
static files directory! These template files are not meant to be directly accessible to end users.
Both of the Pebble templates are renamed to:
base.peb
login.peb
Note the file suffix.
In login.peb
I also changed the extends
directive to refer to the renamed base template:
{% extends "base.peb" %}
The Javalin Routes
To handle the redirect you can use the following routes:
app.get("/", ctx -> {
ctx.redirect("/login");
});
app.get("/login", ctx -> {
ctx.render("login.peb");
});
The /
route redirects to the /login
route. That second route handles rendering of the login.peb
template.
This is why you were seeing a raw template in the browser: The redirect was returning the unrendered template file, instead of returning a route.
Also, by using this approach, the URL displayed in the address bar will be correct: http://localhost:9090/login
Update
I forgot to mention a separate point: I recommend you do not use this:
config.addStaticFiles("src/main/resources/public", Location.EXTERNAL);
That location is not external to the application and anyway you should not force the deployed application to rely on a src
directory. The more typical approach is this:
config.addStaticFiles("/public", Location.CLASSPATH);
(Or, use a truly external path, completely outside of your application's location.)
Upvotes: 1