Friday 27 August 2010

Google AppEngine, Apache Click and NetBeans - Part 2

I have finally managed to get my Hello World application working with Apache Click on AppEngine.

See my previous post which explains how to obtain and compile the latest click libraries here. I did that because I eventually want to try out the ClickClick extensions for Click and their documentation says you need Click Version 2.3.0. However, I am starting to question that, because other parts of the documentation seem out of date.

I am using NetBeans 6.9 with the NBAppEngine plugin. You need to have the AppEngine SDK installed, too.

1. Start a new project using File -> New Project and select category Java Web and project Web Application


2. In the second step, give your project a name, and click next to go to the third step. From the list of servers choose Google App Engine.




3. You do not need to select any frameworks in step 4. When you click Finish your project should look like the one below.



4. To make this a Click project, add the click libraries. Right-click on the Libraries folder and select Add JAR/Folder and select click-2.3.0-M1.jar and click-extras-2.3.0-M1.jar from wherever you saved them.



5. In the Web Pages\WEB-INF folder create a new XML document called click.xml. Later we will create the helloworld.htm view, and the controller class HelloWorld.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE click-app PUBLIC
       "-//Apache Software Foundation//DTD Click Configuration 2.2//EN"
       "http://click.apache.org/dtds/click-2.2.dtd">

<click-app>

  <pages>
      <page path="helloworld.htm" classname="clicktest.HelloWorld"/>
  </pages>

  <mode value="debug"/>

</click-app>


6. Edit the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee" id=""
    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_2_5.xsd">
       
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
   
    <listener>
        <listener-class>org.apache.click.extras.gae.GoogleAppEngineListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>ClickServlet</servlet-name>
        <servlet-class>org.apache.click.ClickServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>   
   
    <servlet-mapping>
        <servlet-name>ClickServlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
   
    <welcome-file-list>
        <welcome-file>helloworld.htm</welcome-file>
    </welcome-file-list>
   
</web-app>


7. Edit the appengine-web.xml file to exclude *.htm files from the list of static file types. See the click documentation for the GoogleAppEngineListener class for more details.

<?xml version="1.0" encoding="UTF-8"?>
<appengine-web-app
  xmlns="http://appengine.google.com/ns/1.0"
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xsi:schemaLocation='http://kenai.com/projects/nbappengine/downloads/download/schema/appengine-web.xsd
  appengine-web.xsd'>
    <application>ClickTest</application>
    <version>1</version>
  
     <!-- Exclude *.htm files from being served as static files by GAE,
             because the *.htm extension is mapped to ClickServlet. -->
     <static-files>
         <exclude path="**.htm" />
     </static-files>     
  
</appengine-web-app>

8. In Web Pages, delete index.jsp and create helloworld.htm. The body of the page should look like this:

<body>
    <h2>Hello World</h2>

    Hello world from Click at $time
</body>

9. Create a new package clicktest with a class HelloWorld

package clicktest;

import java.util.Date;
import org.apache.click.Page;

public class HelloWorld extends Page {

    private Date time = new Date();

    public HelloWorld() {
        addModel("time", time);
    }

}


10. Your project should look like this. Click Run and it should work!


No comments:

Post a Comment