Smart2.0開發指南——入門

說明:本文爲《Smart2.0開發指南》系類文章 javascript

第一步:建立一個基於Servlet3.0的Maven Webapp工程

   打開Eclipse,點擊File>New>Maven Project 選擇項目保存位置,默認爲Workspace目錄,點擊Next, Archetype選擇maven-archetype-webapp,點擊Next,填寫項目信息如圖2-1,點擊Finish完成建立。

標準的Maven Webapp目錄結構以下: html

--src
  |--main
    |--java
    |--resources
    |--webapp
  |--test
    |--java
    |--resources

而這裏使用Eclipse 的Maven插件建立的目錄並不完整,須要咱們手動建立src/main/java、src/test/java、src/test/resources三個目錄,建立後的項目目錄結構如圖2-2。 java

 

   這裏Maven Webapp的Webapp版本爲2.3,也就是Servlet2.3,因爲Smart 是基於Servlet3.0的,因此須要將Webapp的版本改成3.0。在workspace中找到HelloSmart項目,用文本編輯器打開HelloSmart/.settings/org.eclipse.wst.common.project.facet.core.xml ,將<installed facet="java" version="1.5"/> 、<installed facet="jst.web" version="2.3"/>改成<installed facet="java" version="1.7"/> 、<installed facet="jst.web" version="3.0"/>。而後刪掉src/main/webapp/WEB-INF/web.xml文件,或將其內容改成以下代碼: mysql

  <?xml version="1.0" encoding="UTF-8"?>
  <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">
    <display-name>HelloSmart</display-name>
  </web-app>

在Eclipse中打開項目的pom.xml文件,在<build>節點下添加以下配置: web

  <plugins>
  	   <plugin>
  	    <artifactId>maven-compiler-plugin</artifactId>
  	    <version>3.0</version>
  	    <configuration>
  	     <source>1.7</source>
  	     <target>1.7</target>
  	    </configuration>
  	   </plugin>
  </plugins>
選擇HelloSmart項目,右鍵選擇Maven>Update Project更新項目,到這裏,一個基於Servlet3.0的Manve Webapp建立完成。

第二步:引入Smart Framework支持

  咱們是基於Smart Framework開發應用,那麼這裏就須要引入Smart Framework的支持了。由於使用Maven構建項目,那麼Maven會根據pom.xml文件自動下載Smart 的JAR包,而這裏咱們只須要在pom中添加相應的依賴配置便可。依賴配置以下: sql

    <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <smart.version>2.0</smart.version>
    </properties>
    <dependencies>
      	<!-- JUnit -->
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
          </dependency>
          <!-- Servlet -->
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <version>3.0.1</version>
              <scope>provided</scope>
          </dependency>
           <!-- JSTL -->
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
              <scope>runtime</scope>
          </dependency>
           <!-- MySQL -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.25</version>
              <scope>runtime</scope>
          </dependency>
          <!-- Smart -->
          <dependency>
              <groupId>com.smart</groupId>
              <artifactId>smart-framework</artifactId>
              <version>${smart.version}</version>
          </dependency>
    </dependencies>

  接下來須要添加Smart的配置文件,在src/main/resources下建立config.properties,文件中添加以下配置內容: shell

  #應用名稱
  app.name=HelloSmart
  app.package=com.smart.app.hellosmart
  #項目靜態資源目錄
  app.www_path=/www/
  #jsp文件目錄
  app.jsp_path=/WEB-INF/jsp/
  #應用首頁
  app.home_page=/index
  
  jdbc.driver=com.mysql.jdbc.Driver
  jdbc.url=jdbc:mysql://localhost:3306/smart
  jdbc.username=root
  jdbc.password=root
  jdbc.max.active=10
  jdbc.max.idle=10
  #i啓用18n,這裏暫不開啓
  i18n.reloadable=false


  最後還須要配置log4j,在src/main/resources下建立log4j.properties,文件中添加以下配置內容: apache

  log4j.rootLogger=INFO,console,file
  
  log4j.appender.console=org.apache.log4j.ConsoleAppender
  log4j.appender.console.Target=System.out
  log4j.appender.console.layout=org.apache.log4j.PatternLayout
  log4j.appender.console.layout.ConversionPattern=%m%n
  
  log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
  log4j.appender.file.File=${catalina.base}/logs/smart-bootstrap/log
  log4j.appender.file.DatePattern='_'yyyyMMdd
  log4j.appender.file.encoding=UTF-8
  log4j.appender.file.layout=org.apache.log4j.PatternLayout
  log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss,SSS} %p %c (%L) - %m%n
  
  log4j.logger.com.smart.framework=DEBUG
  log4j.logger.com.smart.plugin=DEBUG
  log4j.logger.com.smart.sample=DEBUG

  以上是Smart的最簡單的配置。 bootstrap

第三步:編寫代碼讓Smart飛一會

     首先建立一個歡迎頁面,在app.jsp_path即 /WEB-INF/jsp/ 目錄下建立一個名爲index.jsp的文件,在頁面輸出一個「Hello Smart!」問候語。爲該內容區域註冊一個點擊事件,當點擊該區域後請求/hello資源,並將當前時間作爲一個參數發送給Action處理。Index.jsp代碼以下: api

  <body>
  	<div id="content" >
  		  Hello Smart!
  	</div>
  <script type="text/javascript">
  	var content =  document.getElementById("content");
  	content.addEventListener("click",function (){
  		var date = new Date().getTime();
  		window.location.href = "<%=request.getContextPath()%>/hello?date=" +date;
  	} , false);
  </script>
  </body>

頁面效果如圖2-3。

  接下來編寫一個Action處理來自HTTP GET的請求/hello,在src/main/java/ 建立com.smart.app.hellosmart.action包,而後建立一個名爲HelloSmartAction的類,在類上使用 @action 註解標識該類爲Action,而後建立一個prcoessHello()方法,處理/hello請求,最後將date參數格式化後返回給hello.jsp頁面。HelloSmartAction.java 代碼以下:

  @Action
  public class HelloSmartAction {
  	//請求映射,處理使用GET方法提交的 /hello 請求
  	@Request("GET:/hello") 
  	public Page prcoessHello(Map<String, Object> fieldMap){
  		//fieldMap中取出 date 參數,並使用框架提供的 CastUtil 將其轉換爲Long類型
  		Long date =  CastUtil.castLong(fieldMap.get("date"));
  		//使用框架的DateUtil格式化日期
  		String dateStr = DateUtil.formatDatetime(date);
  		//將處理結果返回到hello.jsp 頁面,並攜帶名爲message的數據
  		return new Page("hello.jsp").data("message", "GET:/hello " + dateStr);
  	}
  }

  在jsp目錄下建立hello.jsp以下:

<body>
  	<div id="content">
  		 <%=request.getAttribute("message") %>
  	</div>
  </body>

  頁面顯示如圖2-4。

  編寫完以上代碼後,將應用部署到Tomcat7中,啓動Tomcat,訪問http://localhost:8080/HelloSmart/ ,將會看到圖2-3的頁面,點擊「Hello Smart」,將請求http://loalhost:8080/HelloSmart/hello,服務器響應如圖2-4頁面。   一個最簡單的基於Smart Framework的應用程序就運行起來了,讓它再飛一會吧!

相關文章
相關標籤/搜索