Skip to content

Instantly share code, notes, and snippets.

@criminy
Created December 26, 2011 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criminy/1520546 to your computer and use it in GitHub Desktop.
Save criminy/1520546 to your computer and use it in GitHub Desktop.
powderj example view.
/**
* Resource which shows the hello world message.
*
* @author sheenobu
*
*/
@Path("/")
public class HelloWorldResource {
// we don't need to load this from a singleton context (yet) because
// the database is just simple, tiny, and in memory.
HelloWorldDatabase db = new HelloWorldDatabase();
@GET
public Response redirect() {
return Response.seeOther(URI.create("/lang/en")).build();
}
/**
* Returns the populated html view of the page.
*
* @param lang The language to display the page in.
* @return The page object.
*/
@GET
@Path("/lang/{lang}")
@Produces("text/html")
public View html(@PathParam(value="lang") String lang) {
return new HelloWorldView(db.getHelloWorldMessage(lang),db.getSupportedLanguages(),lang);
}
}
/**
* The binding view for the hello world page.
*
* @author sheenobu
*
*/
public class HelloWorldView implements View {
private String message;
List<String> languages;
String currentLang;
/**
* Inject in the required data for this view.
*
* @param message
* @param languages
* @param currentLang
*/
public HelloWorldView(String message, List<String> languages,
String currentLang) {
this.message = message;
this.languages = languages;
this.currentLang = currentLang;
}
@Override
public void render(Html html) throws Exception {
html.load(HelloWorldView.class.getResourceAsStream("helloworld.html"));
html.bindValue("#message", message);
html.forEachValue("#languages", languages, new ForEach<String>() {
@Override
public void render(Item<String> item) {
if (item.getObject().equals(currentLang)) {
item.setContent(item.getObject());
} else {
item.setHtml(String.format("<a href='%s'>%s</a>",
item.getObject(), item.getObject()));
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment