JSTL Slide en
JSTL Slide en
JSTL Slide en
awful...
<p>Welcome, member!</p>
<% } else { %>
<p>Welcome, guest!</p>
<% } %>
<fmt:setLocale value="ro"/>
<fmt:setBundle basename="somepackage.Messages"
var="msg"
scope="page"/>
<fmt:message key="hello"
bundle="${msg}"/>
I18N L10N
Resource Bundles
● Property Files
Messages = basename
Messages_ro.properties
# key-value pairs ro = locale
hello = Salut
bye = La revedere
welcome = Bine ai venit, {0} !
→ /WEB-INF/classes/somepackage
● Classes
package somepackage;
import java.util.*;
public class Messages_ro extends ListResourceBundle {
static final Object[][] contents = {
{"hello", "Salut"},
{"bye", "La revedere"}
};
public Object[][] getContents() {
return contents;
}
}
XML Tags (x)
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<x:forEach var="friend"
select="$agenda/friends/person" />
<x:out select="$friend/name" />
</x:forEach>
<sql:setDataSource var="timetable"
dataSource="jdbc/TimetableDB" />
<sql:transaction dataSource="${timetable}">
<table border="2">
<c:forEach var="colName" begin="0" items="${rooms.columnNames}">
<th><b> <c:out value="${colName}" /> </b></th>
</c:forEach>
</table>
</sql:transaction>
Standard Functions (fn)
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/fn" %>
<h1>Welcome ${user}!</h1>
</#list>
</body>
</html>
Example: The Model
(root)
|
+- user = "Big Joe"
|
+- latestProduct
|
+- url = "products/greenmouse.html"
|
+- name = "green mouse"
...
// Choose a template
Template template = cfg.getTemplate("someTemplate.ftl");
out.close();
Using FreeMarker in a Web App
● Register the FreeMarker Servlet
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>
freemarker.ext.servlet.FreemarkerServlet
</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param> ...
</servlet>
● Map the requests
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
● Any request (.ftl) goes to the servlet
http://localhost:8080/myapp/products.ftl
Other alternatives / ideas...
● Google Web Toolkit
– describe the UI using Swing-like components
– GWT translates all the Java code do JS
● Grails
– uses Groovy instead of Java, high-productivity
framework by following the "coding by convention"
paradigm
● Apache Wicket
– plain Java and HTML, mark-up/logic separation,
POJO data model, a refreshing lack of XML
● ...