<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 閱讀(8468) 評論(0)  編輯  收藏 所屬分類: Jetty

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 亚洲福利中文字幕在线网址| 亚洲性色AV日韩在线观看| 国产av无码专区亚洲av毛片搜 | 一个人免费播放在线视频看片| 午夜寂寞在线一级观看免费| 亚洲卡一卡二卡乱码新区| 男女超爽刺激视频免费播放| 亚洲成a人片在线网站| 国产精品久久免费| 亚洲伊人久久大香线蕉影院| 国产精品成人观看视频免费| 亚洲人成www在线播放| 午夜男人一级毛片免费| 无码亚洲成a人在线观看| 国产人成免费视频| 成年免费a级毛片| 亚洲综合精品香蕉久久网| a级毛片毛片免费观看久潮| 国产AV无码专区亚洲AVJULIA| 香港a毛片免费观看| 亚洲欧洲自拍拍偷午夜色| 无人在线观看完整免费版视频| 亚洲精品无码mⅴ在线观看| 国产zzjjzzjj视频全免费 | 免费人成视频在线| 337P日本欧洲亚洲大胆精品| 中文字幕亚洲乱码熟女一区二区| 在线观看免费播放av片| 亚洲国产综合精品| 国产91久久久久久久免费| 国产乱子伦精品免费视频| 久久精品国产亚洲av水果派| 在线免费视频一区二区| 四虎永久在线精品免费一区二区| 亚洲AV永久无码精品一百度影院| 69xx免费观看视频| 国产精品亚洲一区二区三区久久| 国产av无码专区亚洲av果冻传媒| 日本片免费观看一区二区| 黄色免费网址大全| 久久国产亚洲高清观看|