<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xylz,imxylz

    關注后端架構、中間件、分布式和并發編程

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks
    老實說本來這是一篇簡單的RESTful的入門demo,和jetty無關,但是為了方便,用到了jetty的maven插件。勉勉強強算是和jetty有關吧。

    項目地址:https://github.com/adyliu/jetty-rest-demo 

    文件列表


    包含如下文件:

    • git忽略文件
    • README文件
    • pom文件
    • 一個簡單的Controller文件
    • 一個log4j的配置文件
    • 一個簡單的spring mvc配置
    • 一個簡單的web.xml

    Maven配置

    為了能夠方便從jetty:run來啟動web容器,使用jetty的maven插件。
    需要特別注意的是,從jetty7.5.3開始就必須用maven 3了,以前使用的maven 2不能使用了,就為了這問題,我跟蹤了很久,大囧。
    這里使用jetty最新的maven插件,同樣會啟動最新的jetty8.1.1 來測試。
    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.1.v20120215</version>
            </plugin>
        </plugins>
    </build>

    web.xml

    這是一個簡單的web.xml配置,主要配置spring servlet。當然這里也輔助配置了一個log4j,方便查看日志輸出,不配置也沒關系。
    另外也沒有使用servlet 3.0的配置。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version
    ="2.4">
        <display-name>jetty-rest-demo</display-name>
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:log4j.properties</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>

    dispatcher-servlet.xml

    接下來是spring mvc的配置。
    包含三部分:要掃描的住解包,mvc注解驅動以及jsp的渲染映射(其實這個例子中沒有用到)。
    <?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" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    >
        <context:component-scan base-package="info.imxylz.study.jetty.rest" />
        <mvc:annotation-driven />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        </bean>
    </beans>

    Controller

    下載開始寫第一個Controller,當然這個Controller稍微有一點點別扭。
    直接返回字符串,另外將索引頁映射到一個字符串上。(也不對中文進行處理)

    package info.imxylz.study.jetty.rest.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    /**
     * a rest demo (spring 3.x)
     * 
     * 
    @author adyliu (imxylz@gmail.com)
     * 
    @since 2012-3-9
     
    */
    @Controller
    public class DemoController {

        @ResponseBody
        @RequestMapping("/index/{name}/")
        public String index(@PathVariable("name") String name) {
            return "Welcome, " + name;
        }
        @ResponseBody
        @RequestMapping("")
        public String index() {
            return "This is a rest demo";
        }
    }

    Access

    現在該是打開瀏覽器顯示下了。
    http://localhost:8080/index/Ady/
    http://localhost:8080/index/Ady%20Liu/
    http://localhost:8080/

    好了,這算是一個最簡單的入門例子了。
    下面的參考資源中有git的源碼。

    Resources



    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2012-03-09 17:52 imxylz 閱讀(8447) 評論(0)  編輯  收藏 所屬分類: Jetty

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 性做久久久久久久免费看| 日韩免费一区二区三区在线播放| 国产片免费福利片永久| 亚洲人成人网毛片在线播放| 91香蕉成人免费网站| 亚洲六月丁香六月婷婷色伊人 | 57pao一国产成永久免费| 亚洲成AV人片天堂网无码| 日本免费电影一区二区| 精品无码一区二区三区亚洲桃色| 免费黄网站在线看| 亚洲综合网美国十次| 国产成人午夜精品免费视频| 77777午夜亚洲| 国产色爽免费视频| 一边摸一边桶一边脱免费视频| 中文字幕精品无码亚洲字| 中文字幕一区二区免费| 久久久国产精品无码免费专区| 亚洲AV永久纯肉无码精品动漫| 亚洲毛片在线观看| 日日麻批免费40分钟日本的| 亚洲成人午夜电影| 最新猫咪www免费人成| 亚洲精品第一国产综合亚AV| 男人的天堂亚洲一区二区三区 | 久久精品亚洲一区二区| 久操免费在线观看| 亚洲一卡2卡4卡5卡6卡残暴在线| 免费涩涩在线视频网| CAOPORN国产精品免费视频| 中文字幕亚洲综合精品一区| 成人免费看黄20分钟| 中文字幕免费在线看电影大全 | 国产精品亚洲四区在线观看| 亚洲 国产 图片| 9277手机在线视频观看免费| 韩国亚洲伊人久久综合影院| 久久久无码精品亚洲日韩蜜桃 | 亚洲午夜久久久久久尤物| 四只虎免费永久观看|