Commits

Martin Lau  committed bacc05c

added basic portlet

  • Participants
  • Parent commits 3d59581

Comments (0)

Files changed (7)

File src/main/java/ph/hum/sample/basic/controller/BasicController.java

+package ph.hum.sample.basic.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.util.Date;
+
+import static java.lang.System.currentTimeMillis;
+
+@Controller
+public class BasicController {
+
+    @ModelAttribute("current_date")
+    public Date date() {
+        return new Date();
+    }
+
+    @ModelAttribute("offset_date")
+    public Date dateWithOffset(@RequestParam(required = false, value = "offset") final Long offset) {
+        if (offset == null) {
+            return null;
+        }
+        return new Date(currentTimeMillis() + offset);
+    }
+
+    @RequestMapping("VIEW")
+    public String renderView() {
+        return "basic/view.jspx";
+    }
+
+}

File src/main/webapp/WEB-INF/basic-portlet.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
+
+    <context:component-scan base-package="ph.hum.sample.basic"/>
+
+</beans>

File src/main/webapp/WEB-INF/liferay-display.xml

 
 <display>
     <category name="category.sample">
+        <portlet id="basic"/>
         <portlet id="knownClass"/>
         <portlet id="localService"/>
         <portlet id="requestAttribute"/>

File src/main/webapp/WEB-INF/liferay-portlet.xml

 <liferay-portlet-app>
 
     <portlet>
+        <portlet-name>basic</portlet-name>
+        <instanceable>false</instanceable>
+    </portlet>
+
+    <portlet>
         <portlet-name>knownClass</portlet-name>
         <instanceable>false</instanceable>
     </portlet>

File src/main/webapp/WEB-INF/portlet.xml

              version="2.0">
 
     <portlet>
+        <portlet-name>basic</portlet-name>
+        <display-name>basic</display-name>
+        <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
+        <expiration-cache>0</expiration-cache>
+        <supports>
+            <mime-type>text/html</mime-type>
+            <portlet-mode>view</portlet-mode>
+        </supports>
+        <portlet-info>
+            <title>basic</title>
+            <short-title>basic</short-title>
+            <keywords>basic</keywords>
+        </portlet-info>
+        <security-role-ref>
+            <role-name>administrator</role-name>
+        </security-role-ref>
+        <security-role-ref>
+            <role-name>guest</role-name>
+        </security-role-ref>
+        <security-role-ref>
+            <role-name>power-user</role-name>
+        </security-role-ref>
+        <security-role-ref>
+            <role-name>user</role-name>
+        </security-role-ref>
+    </portlet>
+
+    <portlet>
         <portlet-name>knownClass</portlet-name>
         <display-name>knownClass</display-name>
         <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>

File src/main/webapp/WEB-INF/servlet/view/portal/basic/view.jspx

+<?xml version="1.0" encoding="UTF-8"?>
+
+<jsp:root xmlns="http://www.w3.org/1999/xhtml"
+          xmlns:c="http://java.sun.com/jsp/jstl/core"
+          xmlns:jsp="http://java.sun.com/JSP/Page"
+          xmlns:portlet="http://java.sun.com/portlet"
+          version="2.0">
+
+    <h3>A model attribute</h3>
+
+    <p>
+        ${current_date}
+    </p>
+
+    <c:if test="${not empty offset_date}">
+        <h3>An offset date</h3>
+
+        <p>
+            ${offset_date}
+        </p>
+    </c:if>
+
+    <portlet:renderURL var="offset_url">
+        <portlet:param name="offset" value="${1000 * 60 * 60 * 24 * 182}" />
+    </portlet:renderURL>
+    <p>
+        <a href="${offset_url}">A date with a 6 month offset from now</a>
+    </p>
+
+</jsp:root>

File src/test/java/ph/hum/sample/basic/controller/BasicControllerTest.java

+package ph.hum.sample.basic.controller;
+
+import org.junit.Test;
+
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class BasicControllerTest {
+
+    @Test
+    public void testDate() throws Exception {
+        Date now = new Date();
+        BasicController controller = new BasicController();
+
+        Date result = controller.date();
+
+        assertThat(result.getTime() - now.getTime()).isLessThanOrEqualTo(1000L);
+    }
+
+    @Test
+    public void testDateWithOffset() throws Exception {
+        Date now = new Date();
+        BasicController controller = new BasicController();
+
+        Date result = controller.dateWithOffset(1000000L);
+
+        assertThat(result.getTime() - now.getTime()).isLessThanOrEqualTo(1000000L + 1000L);
+    }
+
+    @Test
+    public void testDateWithOffsetWithNull() throws Exception {
+        BasicController controller = new BasicController();
+
+        Date result = controller.dateWithOffset(null);
+
+        assertThat(result).isNull();
+    }
+
+    @Test
+    public void testRenderView() throws Exception {
+        BasicController controller = new BasicController();
+
+        String result = controller.renderView();
+
+        assertThat(result).isEqualTo("basic/view.jspx");
+    }
+
+}