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

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

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

    xylz,imxylz

    關(guān)注后端架構(gòu)、中間件、分布式和并發(fā)編程

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

    項(xiàng)目地址:https://github.com/adyliu/jetty-rest-demo 

    文件列表


    包含如下文件:

    • git忽略文件
    • README文件
    • pom文件
    • 一個(gè)簡(jiǎn)單的Controller文件
    • 一個(gè)log4j的配置文件
    • 一個(gè)簡(jiǎn)單的spring mvc配置
    • 一個(gè)簡(jiǎn)單的web.xml

    Maven配置

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

    web.xml

    這是一個(gè)簡(jiǎn)單的web.xml配置,主要配置spring servlet。當(dāng)然這里也輔助配置了一個(gè)log4j,方便查看日志輸出,不配置也沒關(guān)系。
    另外也沒有使用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注解驅(qū)動(dòng)以及jsp的渲染映射(其實(shí)這個(gè)例子中沒有用到)。
    <?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

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

    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

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

    好了,這算是一個(gè)最簡(jiǎn)單的入門例子了。
    下面的參考資源中有g(shù)it的源碼。

    Resources



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

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 在线日韩日本国产亚洲| 国产精品亚洲mnbav网站| 精品亚洲aⅴ在线观看| 免费福利电影在线观看| 亚洲国产精品SSS在线观看AV| 国产一级一毛免费黄片| 中文字幕亚洲日韩无线码| 一级特黄录像免费播放肥| 精品国产香蕉伊思人在线在线亚洲一区二区| 相泽南亚洲一区二区在线播放| 国产色婷婷精品免费视频| 乱爱性全过程免费视频| 永久亚洲成a人片777777| 国产在线一区二区综合免费视频| 亚洲视频精品在线| 在线观看免费人成视频色| 亚洲码欧美码一区二区三区| 国产一区二区三区免费在线观看| 免费精品久久久久久中文字幕 | 亚洲日韩精品无码专区加勒比☆| 99热在线精品免费全部my| 国产精品无码亚洲一区二区三区| 免费国产人做人视频在线观看| sihu国产精品永久免费| 青青草原精品国产亚洲av| 两个人的视频高清在线观看免费| 免费福利资源站在线视频| 久久亚洲国产中v天仙www| 成年人免费的视频| 日韩在线一区二区三区免费视频| 亚洲va中文字幕无码久久| 中文字幕av无码无卡免费| 欧洲精品码一区二区三区免费看| 亚洲国产成人久久精品动漫 | 亚洲国产精品无码久久久秋霞2| 99久久人妻精品免费一区| 亚洲国产高清国产拍精品| 国产亚洲一区二区三区在线观看| 国色精品卡一卡2卡3卡4卡免费| 一道本在线免费视频| 亚洲性69影院在线观看|