最近由于項目調整接手了個維護項目,仔細一看比較崩潰。一是由于項目維護人員很少,二是系統使用的技術比較舊,使用傳統的Servlet+JSP開發,而且Sevlet代碼中充斥着大段的PrintOut出來的Jsp代碼,JSP代碼中同樣充斥着大量的JAVA代碼。如果有新的需求變更,修改起來代價很大。
恰好這幾天整理個人電腦時發現當時第一次參加工作時的項目源碼,當時的公司采用了SpringMVC+Click+Cayenne整合了一套開發架構,當時開發的時候并沒有花心思去仔細了解;現在想來當時Click是比較流行的時候,現在閑來無事,花點時間整理一下,權當是一種休閑娛樂吧!
閑話少說,下面就使用Click開發一個最簡單的Web應用。
(一)準備工作
- 從Apache官網上下載下傳click-2.3.0.zip,解壓到本地磁盤;
- 使用Eclipse建立Web應用click-sample,将click-2.3.0中dist中的click-2.3.0.jar和click-extras-2.3.0.jar導入到Web-INF中的lib裡;
(二)代碼編寫
1,建立HelloWorldPage.java,繼承至org.apache.click.Page; 把message變量添加到頁面模型的“message”中。Click 會確定所有添加到頁面模型中的對象都在頁面模版中可用。
- package com.jonny.click.sample.page;
- import org.apache.click.Page;
- public class HelloWorldPage extends Page{
- private static final long serialVersionUID = -3770478730293725405L;
- private String message = "HelloWorld";
- public HelloWorldPage() {
- addModel("message", message);
- }
- }
2, 在WebContent下建立hello-world.htm,使用$message來擷取頁面的message變量;
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>HelloWorld</title>
- </head>
- <body>
- $message
- </body>
- </html>
3,修改WEB-INF下的web.xml檔案;另外新增click.xml;
web.xml
所有Click 頁面模闆必須用.htm 擴充名,并且,ClickServlet 應該比對來處理所有*.htm 的URL 請求。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>click-sample</display-name>
- <welcome-file-list>
- <welcome-file>hello-world.htm</welcome-file>
- </welcome-file-list>
- <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>
- </web-app>
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <click-app charset="UTF-8">
- <pages package="com.jonny.click.sample.page"/>
- <mode value="profile"/>
- </click-app>