Scripting

Jease is easily extensible directly through the CMS by running scripts which can do all kinds of dynamic stuff. Simply add an Script-Object to Jease and give it an id with a proper extension denoting the kind of script contained in the object.

JavaServerPages

Out of the box you can use JavaServerPages (with extension ".jsp"). Just create a Script object, name it News.jsp and enter the following code as an example which creates a listing of all published news:

<%@page import="jease.cms.domain.*,jease.site.*"%>
<% for(News news : Navigations.getSiteNews()) { %>
 <h2><%=news.getTitle()%> (<%= String.format("%tF", news.getDate())%>)</h2>
 <p>
   <%=news.getTeaser()%>
   <br />
   <a href="<%=news.getPath()%>?print">Read more...</a>
 </p> 
<% } %>

Another example is to create a sitemap as XML-file for submission to your search engine of choice. Create this script with the "Forward"-attribute checked, so the content is returned directly (without being rendered in the page layout).

<?xml version="1.0" encoding="UTF-8"?>
<%@page import="jfix.servlet.*,jease.cms.domain.*,jease.cmf.service.*" 
        contentType="text/xml; charset=utf-8" %>
<urlset
 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<%
  String domain = (request.isSecure() ? "https://" : "http://") +  Servlets.getHost(request) + request.getContextPath();
  for(Content content : Nodes.getRoot().getDescendants(Content.class)) { 
      if(content.isVisible()) {              
%>
 <url>
  <loc><%=domain %><%=content.getPath()%></loc>
  <lastmod><%=String.format("%tF",content.getLastModified())%></lastmod>
 </url>
<% } %>
<% } %>
</urlset>

Servlets

Another option which is provided out of the box are dynamic servlets, which don't need to be configured in your web.xml. Just create a Script-Object with the extension ".java" and put the code of your servlet into it. The rest is handled by a Java-Compiler which compiles and runs the servlet automatically.

To test it out, just add a Script-Object with id=HelloServlet.java and put the following code into it:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
    throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.print("Hello " + request.getParameter("name"));
  }
}

Now you can call the servlet and feed it via a request parameter (e.g. http://localhost:8080/HelloServlet.java?name=Jease).

ZUL Markup

Jease allows also the seamless integration of ZUML - the ZK based markup language (use ".zul" as extension). Below is an example for a simple contact form. Just create a Script with id=contact.zul and copy the code below into it.

You can also have a look at more [./~/demo/zul ZUL showcases].

<vlayout width="100%"> 
  Name:
  <textbox id="name" hflex="1" constraint="no empty"/> 
  Email:
  <textbox id="email" hflex="1" constraint="/.+@.+\.[a-z]+/: No valid address" />
  Message:
  <textbox id="message" hflex="1" rows="10" constraint="no empty" /> 
  <button label="Send Message"> 
    <attribute name="onClick"><![CDATA[ 
      if(name.isValid() && email.isValid() && message.isValid()) { 
        jease.cms.service.Mails.send(
          email.value,
          "[email protected]",
          "Message from " + name.value + " <" + email.value + ">",
          message.value
        );
        name.rawValue = email.rawValue = message.rawValue = null;
        Messagebox.show("We have received your message.", "Thank you!",
                        Messagebox.OK, Messagebox.INFORMATION);
      } else {
        Messagebox.show("All fields are required!", "Error", 
                        Messagebox.OK, Messagebox.ERROR);
      }
    ]]></attribute>
 </button> 
</vlayout>

Adding other scripting languages

If you want to add support for another scripting language (e.g. Groovy, Velocity, Freemarker), you'll have to configure a servlet which handles the scripting language.

Here's an exmample for adding Groovy support to Jease:

  • Download Groovy distribution and copy "groovy/embeddable/groovy-all.jar" to Jease/WEB-INF/lib.
  • Edit Jease/WEB-INF/web.xml to add support for the GroovyTemplateServlet:
<servlet>
 <servlet-name>GroovyTemplateServlet</servlet-name>
 <servlet-class>
  groovy.servlet.TemplateServlet
 </servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>GroovyTemplateServlet</servlet-name>
 <url-pattern>*.groovy</url-pattern>
</servlet-mapping>

Now you can use the content-type "Script" to execute GroovyTemplates. All you have to do is to specify the appropriate extension (.groovy) as id for the Script.

Example "Sitemap.groovy"

<% import jease.cmf.service.*; %>
<% 
 Nodes.root.descendants.findAll{it.visible}.each {
  parents = it.parents.length;
  style = parents == 1 ? "font-weight: bold;" : 
                "margin-left: " + parents + "em;";
%>
  <a href="<%= it.path %>" style="<%= style %>">
   <%= it.title %>
  </a>
  <br />
<% } %>
Last modified on 2011-05-26 by Maik Jablonski

Want to contribute?

2009-12-31

Do you want to contribute to Jease? Writing documentation is a good place to get started and will help to get people becoming more familiar with Jease. Any contribution is appreciated.