ROSEhtml
2.1 基礎環境java
* 普通的pc機,del 380
* ubuntu 10.04基本不升級
* java version "1.6.0_29"
* eclipse
* m2clipse
* 茶一杯git
2.2 maven簡介github
* maven是基於項目對象模型(POM),能夠經過一小段描述信息來管理項目的構建,報告和文檔的軟件項目管理工具。若是你已經有十次輸入一樣的Ant targets來編譯你的代碼、jar或者war、生成javadocs,你必定會自問,是否有一個重複性更少卻能一樣完成該工做的方法。Maven便提供了這樣一種選擇,將你的注意力從做業層轉移到項目管理層。Maven項目已經可以知道如何構建和捆綁代碼,運行測試,生成文檔並宿主項目網頁。
* maven對一個項目進入了固定的默認目錄定義:
* src/main/java 寫主要的java實現
* src/main/resources 寫主要的配置文件
* src/test/java 寫test case
* src/test/resources 寫test case所須要的配置文件
* src/main/webapp [war項目特有]web項目的對外目錄
* src/main/webapp/WEB-INF [war項目特有]web項目配置web.xml目錄web
2.3 項目創建spring
* 打開eclipse(須要提早安裝好m2clipse插件)
* new -> other -> maven -> maven project
* create a simple project
* next
* group id:com.54chen
* artifact id:rose-example
* packaging: war
* finishedapache
2.4 基礎配置三步走ubuntu
1)點火:基礎的pom文件
打開2.3創建好的項目,打開pom.xml,添加下面的段落到project中:瀏覽器
- <dependencies>
- <dependency>
- <groupId>com.54chen</groupId>
- <artifactId>paoding-rose-scanning</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.54chen</groupId>
- <artifactId>paoding-rose</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.54chen</groupId>
- <artifactId>paoding-rose-portal</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.54chen</groupId>
- <artifactId>paoding-rose-jade</artifactId>
- <version>1.1-SNAPSHOT</version>
- </dependency>
- </dependencies>
上述是rose環境最基礎的依賴包。再添加一點常見的編譯設置:app
- <build>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.*</include>
- </includes>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.0.2</version>
- <configuration>
- <webResources>
- <resource>
- <targetPath>WEB-INF</targetPath>
- <filtering>true</filtering>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.xml</include>
- <include>**/*.properties</include>
- </includes>
- <targetPath>WEB-INF</targetPath>
- </resource>
- </webResources>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <fork>true</fork>
- <verbose>true</verbose>
- <encoding>UTF-8</encoding>
- <compilerArguments>
- <sourcepath>
- ${project.basedir}/src/main/java
- </sourcepath>
- </compilerArguments>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <!-- 忽略測試 -->
- <skip>true</skip>
- </configuration>
- </plugin>
- </plugins>
- </build>
上述是編譯設置,也是放在project段落裏。
2)鬆離合:必不可少的web.xml
在src/main/webapp/WEB-INF文件夾下創建web.xml:
- <?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>rose-example</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <filter>
- <filter-name>roseFilter</filter-name>
- <filter-class>net.paoding.rose.RoseFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>roseFilter</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- <dispatcher>INCLUDE</dispatcher>
- </filter-mapping>
- </web-app>
3)踩油門:applicationContext.xml
src/main/resources/applicationContext.xml是spring環境的油門,全部包的掃描和啓動都在這裏定義:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd"
- default-lazy-init="true">
- <!-- 自動掃描 -->
- <context:annotation-config />
- <context:component-scan base-package="com.chen">
- </context:component-scan>
- </beans>
2.5 hello world
* 在src/main/java上右鍵 -> new -> package -> name: com.chen
* 在com.chen上右鍵 -> new -> package -> com.chen.controllers [controllers是rose框架默認的加載controller的package name]
* 在com.chen.controllers上右鍵 -> new -> class -> HelloController [*Controller是rose框架默認的controller層的class後綴]
* 打開HelloController這個類
* 在public class HelloController添加註解@Path("") [Path註解是rose框架提供的標識每一個controller的對外訪問時的基礎路徑]
* 在HelloController中添加方法
- /**
- * @author 54chen(陳臻) [chenzhen@xiaomi.com czhttp@gmail.com]
- * @since 2012-4-10 上午11:14:46
- */
- package com.chen.controllers;
- import net.paoding.rose.web.annotation.Path;
- import net.paoding.rose.web.annotation.rest.Get;
- @Path("")
- public class HelloController {
- @Get("")
- public String index() {
- return "@hello world";
- }
- }
* [Get註解是rose框架提供的標識一個http訪問是get仍是post或者是其餘,而且會將path與get中的字符串鏈接成一個url]
* 上述代碼能夠從瀏覽器訪問:http://localhost/。
* 下述代碼能夠從瀏覽器訪問:http://localhost/hello/world [注意path與get中的參數]。
- /**
- * @author 54chen(陳臻) [chenzhen@xiaomi.com czhttp@gmail.com]
- * @since 2012-4-10 上午11:14:46
- */
- package com.chen.controllers;
- import net.paoding.rose.web.annotation.Path;
- import net.paoding.rose.web.annotation.rest.Get;
- @Path("/hello/")
- public class HelloController {
- @Get("world")
- public String index() {
- return "@hello world";
- }
- }
__EOF__
* 動態更新版本地址在:https://github.com/XiaoMi/rose/tree/master/chapter_2
* 文中所說起的代碼在:https://github.com/XiaoMi/rose/rose-example
原創文章如轉載,請註明:轉載自五四陳科學院[http://www.54chen.com]
本文連接: http://www.54chen.com/java-ee/rose-manual-2.html