Created
December 26, 2011 04:51
-
-
Save criminy/1520546 to your computer and use it in GitHub Desktop.
powderj example view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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