Skip to content

Servlet for serving Mustache based templates with backing Java code

Notifications You must be signed in to change notification settings

spullara/mustachelet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

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>

About

Servlet for serving Mustache based templates with backing Java code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages