fersab/mustachelet
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
Mustache gives you a great way to generate HTML, this wraps that up in a Servlet so
you can do it quite easily. Here is an example:
Backing code (Index.java):
@Path("/")
@Template("index.html")
public class Index {
@Controller
boolean exists() {
return true;
}
String name() {
return "Sam";
}
}
Template code (index.html):
<html>
<head>
<title>Index</title>
</head>
<body>
Hello, {{name}}!
</body>
</html>
The POST use case is a little more involved. Here is the post mustachelet:
@Path("/post(/(.*))?")
@Template("post.html")
@HttpMethod({GET, POST})
public class Post {
@Request(RESPONSE)
HttpServletResponse response;
@Request(REQUEST)
HttpServletRequest request;
@Controller(POST)
boolean redirectPostData() throws IOException {
response.sendRedirect("/post/" + request.getParameter("value"));
return false;
}
@Request(MATCHER)
Matcher m;
String value() {
return m.group(2);
}
}
With its associated template:
<html>
<head>
<title>Post</title>
</head>
<body>
<form method="POST" action="/post">
<input type="text" name="value" value="{{value}}"><input type="submit">
</form>
</body>
</html>