zoukankan      html  css  js  c++  java
  • rose学习

    转载自五四陈科学院[http://www.54chen.com

    rose手册第二章:配置与使用

    ROSE

    2.1 基础环境

    * 普通的pc机,del 380
    * ubuntu 10.04基本不升级
    * java version "1.6.0_29"
    * eclipse
    * m2clipse
    * 茶一杯

    2.2 maven简介

    * 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目录

    2.3 项目建立

    * 打开eclipse(需要提前安装好m2clipse插件)
    * new -> other -> maven -> maven project
    * create a simple project
    * next
    * group id:com.54chen
    * artifact id:rose-example
    * packaging: war
    * finished

    2.4 基础配置三步走

    1)点火:基础的pom文件 
    打开2.3建立好的项目,打开pom.xml,添加下面的段落到project中:

     
    1. <dependencies>  
    2.         <dependency>  
    3.             <groupId>com.54chen</groupId>  
    4.             <artifactId>paoding-rose-scanning</artifactId>  
    5.             <version>1.0-SNAPSHOT</version>  
    6.         </dependency>  
    7.   
    8.         <dependency>  
    9.             <groupId>com.54chen</groupId>  
    10.             <artifactId>paoding-rose</artifactId>  
    11.             <version>1.0-SNAPSHOT</version>  
    12.         </dependency>  
    13.   
    14.         <dependency>  
    15.             <groupId>com.54chen</groupId>  
    16.             <artifactId>paoding-rose-portal</artifactId>  
    17.             <version>1.0-SNAPSHOT</version>  
    18.         </dependency>  
    19.   
    20.         <dependency>  
    21.             <groupId>com.54chen</groupId>  
    22.             <artifactId>paoding-rose-jade</artifactId>  
    23.             <version>1.1-SNAPSHOT</version>  
    24.         </dependency>  
    25.     </dependencies>  

    上述是rose环境最基础的依赖包。再添加一点常见的编译设置:

     
    1. <build>  
    2.     <resources>  
    3.         <resource>  
    4.             <directory>src/main/resources</directory>  
    5.             <includes>  
    6.                 <include>**/*.*</include>  
    7.             </includes>  
    8.         </resource>  
    9.     </resources>  
    10.     <plugins>  
    11.         <plugin>  
    12.             <groupId>org.apache.maven.plugins</groupId>  
    13.             <artifactId>maven-war-plugin</artifactId>  
    14.             <version>2.0.2</version>  
    15.             <configuration>  
    16.                 <webResources>  
    17.                     <resource>  
    18.                         <targetPath>WEB-INF</targetPath>  
    19.                         <filtering>true</filtering>  
    20.                         <directory>src/main/resources</directory>  
    21.                         <includes>  
    22.                             <include>**/*.xml</include>  
    23.                             <include>**/*.properties</include>  
    24.                         </includes>  
    25.                         <targetPath>WEB-INF</targetPath>  
    26.                     </resource>  
    27.                 </webResources>  
    28.             </configuration>  
    29.         </plugin>  
    30.         <plugin>  
    31.             <groupId>org.apache.maven.plugins</groupId>  
    32.             <artifactId>maven-compiler-plugin</artifactId>  
    33.             <configuration>  
    34.                 <source>1.6</source>  
    35.                 <target>1.6</target>  
    36.                 <fork>true</fork>  
    37.                 <verbose>true</verbose>  
    38.                 <encoding>UTF-8</encoding>  
    39.                 <compilerArguments>  
    40.                     <sourcepath>  
    41.                         ${project.basedir}/src/main/java  
    42.                        </sourcepath>  
    43.                 </compilerArguments>  
    44.             </configuration>  
    45.         </plugin>  
    46.         <plugin>  
    47.             <groupId>org.apache.maven.plugins</groupId>  
    48.             <artifactId>maven-surefire-plugin</artifactId>  
    49.             <configuration>  
    50.                 <!-- 忽略测试 -->  
    51.                 <skip>true</skip>  
    52.             </configuration>  
    53.         </plugin>  
    54.     </plugins>  
    55. </build>  

    上述是编译设置,也是放在project段落里。

    2)松离合:必不可少的web.xml 
    在src/main/webapp/WEB-INF文件夹下建立web.xml:

     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    5.     id="WebApp_ID" version="2.5">  
    6.     <display-name>rose-example</display-name>  
    7.     <welcome-file-list>  
    8.         <welcome-file>index.html</welcome-file>  
    9.         <welcome-file>index.htm</welcome-file>  
    10.         <welcome-file>index.jsp</welcome-file>  
    11.         <welcome-file>default.html</welcome-file>  
    12.         <welcome-file>default.htm</welcome-file>  
    13.         <welcome-file>default.jsp</welcome-file>  
    14.     </welcome-file-list>  
    15.       
    16.        
    17.     <context-param>  
    18.         <param-name>log4jConfigLocation</param-name>  
    19.         <param-value>/WEB-INF/log4j.xml</param-value>  
    20.     </context-param>  
    21.   
    22.     <listener>  
    23.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    24.     </listener>  
    25.     <filter>  
    26.         <filter-name>roseFilter</filter-name>  
    27.         <filter-class>net.paoding.rose.RoseFilter</filter-class>  
    28.     </filter>   
    29.     <filter-mapping>  
    30.         <filter-name>roseFilter</filter-name>  
    31.         <url-pattern>/*</url-pattern>  
    32.         <dispatcher>REQUEST</dispatcher>  
    33.         <dispatcher>FORWARD</dispatcher>  
    34.         <dispatcher>INCLUDE</dispatcher>  
    35.     </filter-mapping>   
    36. </web-app>  

    3)踩油门:applicationContext.xml 
    src/main/resources/applicationContext.xml是spring环境的油门,所有包的扫描和启动都在这里定义:

     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
    5.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    6.     http://www.springframework.org/schema/context    
    7.     http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
    8.     default-lazy-init="true">  
    9.   
    10.     <!-- 自动扫描 -->  
    11.     <context:annotation-config />  
    12.     <context:component-scan base-package="com.chen">  
    13.     </context:component-scan>  
    14.        
    15. </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中添加方法

     
    1. /** 
    2.  * @author 54chen(陈臻) [chenzhen@xiaomi.com czhttp@gmail.com] 
    3.  * @since 2012-4-10 上午11:14:46 
    4.  */  
    5. package com.chen.controllers;  
    6.   
    7. import net.paoding.rose.web.annotation.Path;  
    8. import net.paoding.rose.web.annotation.rest.Get;  
    9.   
    10. @Path("")  
    11. public class HelloController {  
    12.   
    13.     @Get("")  
    14.     public String index() {  
    15.         return "@hello world";  
    16.     }  
    17. }  

    * [Get注解是rose框架提供的标识一个http访问是get还是post或者是其他,并且会将path与get中的字符串连接成一个url]
    * 上述代码可以从浏览器访问:http://localhost/。
    * 下述代码可以从浏览器访问:http://localhost/hello/world [注意path与get中的参数]。

     
    1. /** 
    2.  * @author 54chen(陈臻) [chenzhen@xiaomi.com czhttp@gmail.com] 
    3.  * @since 2012-4-10 上午11:14:46 
    4.  */  
    5. package com.chen.controllers;  
    6.   
    7. import net.paoding.rose.web.annotation.Path;  
    8. import net.paoding.rose.web.annotation.rest.Get;  
    9.   
    10. @Path("/hello/")  
    11. public class HelloController {  
    12.   
    13.     @Get("world")  
    14.     public String index() {  
    15.         return "@hello world";  
    16.     }  
    17. }  

    __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

    This entry was posted in java and tagged . Bookmark the permalink.

    7 Responses to “rose手册第二章:配置与使用”

  • 相关阅读:
    zipfile和tarfile的简单使用方法
    RabbitMQ安装
    postman接口自动化
    linux命令
    redis安装部署和使用
    nmon使用
    jdk自带监控工具配置使用
    修改本机mac
    hashlib模块,md5加密
    tomcat部署
  • 原文地址:https://www.cnblogs.com/--Candice/p/4384391.html
Copyright © 2011-2022 走看看