6.1.11. Juzu portlet

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

The source code used in this tutorial is here.

Juzu framework offers the following features to ease portlet development:

References

This tutorial focuses on Juzu portlet deployment in eXo Platform Tomcat and JBoss.

The dependencies are different for each server and each dependency injection implementation, so the project will use different Maven build profiles for packaging in each case:

Note

Currently, only Guice and Spring are covered in this tutorial. The other implementation, Weld, will be documented later.

  1. Create a Maven project as follows:

    Note

    You can use the following archetype to generate project but make sure you will modify every single file in accordance with this tutorial.

    mvn archetype:generate -DarchetypeGroupId=org.juzu -DarchetypeArtifactId=juzu-archetype -DarchetypeVersion=1.0.0 -DgroupId=org.exoplatform.samples -DartifactId=hellojz -Dversion=4.2.x

  2. Edit Controller.java:

    package org.exoplatform.samples;
    
    
    import juzu.Path;
    import juzu.View;
    import juzu.Response;
    import juzu.template.Template;
    import javax.inject.Inject;
    import java.io.IOException;
    public class Controller {
      @Inject
      @Path("index.gtmpl")
      Template index;
      @View
      public Response.Content index() throws IOException {
        return index.ok();
      }
    }
  3. Edit package-info.java:

    @juzu.Application
    
    @juzu.plugin.servlet.Servlet(value = "/")
    package org.exoplatform.samples;
  4. Edit index.gtmpl:

    Hello World
  5. Edit jboss-deployment-structure.xml:

    
    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
        <deployment>
            <dependencies>
                <module name="deployment.platform.ear" export="true"/>
            </dependencies>
        </deployment>
    </jboss-deployment-structure>
  6. Edit portlet.xml:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
        <portlet>
            <portlet-name>SampleApplication</portlet-name>
            <display-name xml:lang="EN">Juzu Sample Application</display-name>
            <portlet-class>juzu.bridge.portlet.JuzuPortlet</portlet-class>
            <init-param>
                <name>juzu.app_name</name>
                <value>org.exoplatform.samples</value>
            </init-param>
            <supports>
                <mime-type>text/html</mime-type>
            </supports>
            <portlet-info>
                <title>Sample Application</title>
            </portlet-info>
        </portlet>
    </portlet-app>
  7. Edit web-guice.xml:

    
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <!-- Run mode: prod, dev or live -->
        <context-param>
            <param-name>juzu.run_mode</param-name>
            <param-value>${juzu.run_mode:dev}</param-value>
        </context-param>
        <!-- Injection container to use: guice, spring, cdi or weld -->
        <context-param>
            <param-name>juzu.inject</param-name>
            <param-value>guice</param-value>
        </context-param>
    </web-app>
  8. Edit web-spring.xml:

    
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
    <!-- Run mode: prod, dev or live -->
        <context-param>
            <param-name>juzu.run_mode</param-name>
            <param-value>${juzu.run_mode:dev}</param-value>
        </context-param>
        <!-- Injection container to use: guice, spring, cdi or weld -->
        <context-param>
            <param-name>juzu.inject</param-name>
            <param-value>spring</param-value>
        </context-param>
    </web-app>
  9. Edit pom.xml:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.exoplatform.samples</groupId>
        <artifactId>hellojz</artifactId>
        <version>4.2.x</version>
        <packaging>war</packaging>
        <name>Juzu Application</name>
        <properties>
            <maven.compiler.target>1.6</maven.compiler.target>
            <maven.compiler.source>1.6</maven.compiler.source>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.juzu</groupId>
                <artifactId>juzu-core</artifactId>
                <version>1.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.juzu</groupId>
                <artifactId>juzu-plugins-servlet</artifactId>
                <version>1.0.0</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>hellojz</finalName>
        </build>
        <profiles>
            <profile>
                <id>plf-tomcat-guice</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                    <property>
                        <name>target</name>
                        <value>plf-tomcat-guice</value>
                    </property>
                </activation>
                <dependencies>
                    <dependency>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                        <version>3.0</version>
                    </dependency>
                </dependencies>
                <properties>
                    <maven.war.webxml>src/main/web-guice.xml</maven.war.webxml>
                </properties>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-war-plugin</artifactId>
                            <version>2.6</version>
                            <configuration>
                                <packagingExcludes>
                                    WEB-INF/jboss-deployment-structure.xml,
                                    WEB-INF/lib/*.jar
                                </packagingExcludes>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>plf-jboss-guice</id>
                <activation>
                    <property>
                        <name>target</name>
                        <value>plf-jboss-guice</value>
                    </property>
                </activation>
                <dependencies>
                    <dependency>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                        <version>3.0</version>
                    </dependency>
                </dependencies>
                <properties>
                    <maven.war.webxml>src/main/web-guice.xml</maven.war.webxml>
                </properties>
            </profile>
            <profile>
                <id>plf-tomcat-spring</id>
                <activation>
                    <property>
                        <name>target</name>
                        <value>plf-tomcat-spring</value>
                    </property>
                </activation>
                <dependencies>
                    <dependency>
                        <groupId>javax.inject</groupId>
                        <artifactId>javax.inject</artifactId>
                        <version>1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <scope>runtime</scope>
                        <version>2.5.5</version>
                    </dependency>
                </dependencies>
                <properties>
                    <maven.war.webxml>src/main/web-spring.xml</maven.war.webxml>
                </properties>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-war-plugin</artifactId>
                            <version>2.6</version>
                            <configuration>
                                <packagingExcludes>
                                    WEB-INF/jboss-deployment-structure.xml
                                </packagingExcludes>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>plf-jboss-spring</id>
                <activation>
                    <property>
                        <name>target</name>
                        <value>plf-jboss-spring</value>
                    </property>
                </activation>
                <dependencies>
                    <dependency>
                        <groupId>javax.inject</groupId>
                        <artifactId>javax.inject</artifactId>
                        <version>1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <scope>runtime</scope>
                        <version>2.5.5</version>
                    </dependency>
                </dependencies>
                <properties>
                    <maven.war.webxml>src/main/web-spring.xml</maven.war.webxml>
                </properties>
            </profile>
        </profiles>
    </project>

Here are some remarks:

Dependencies

Juzu:


<dependency>
    <groupId>org.juzu</groupId>
    <artifactId>juzu-core</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.juzu</groupId>
    <artifactId>juzu-plugins-servlet</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
</dependency>

Guice:


<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>3.0</version>
</dependency>

Spring:


<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>2.5.5</version>
</dependency>

Note that you can deploy this portlet using Guice in Tomcat and JBoss or using Spring in Tomcat as usual. However, to deploy this portlet using Spring in JBoss, the following dependencies are needed at runtime:

These dependencies have been already packaged in the generated hellojz.war file. Therefore you can choose to deploy this portlet via the 2 following ways:

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus