<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
    主站蜘蛛池模板: 国产福利在线观看永久免费| 久久九九免费高清视频| 午夜理伦剧场免费| 久久亚洲精品AB无码播放| 中文字幕不卡免费视频| 国产AV无码专区亚洲AV手机麻豆| 国产亚洲精彩视频| 亚洲欧洲一区二区三区| 精品国产污污免费网站入口| 亚洲精品国产美女久久久| 久久精品电影免费动漫| 免费高清小黄站在线观看| 好看的电影网站亚洲一区| 免费无码作爱视频| 亚洲网站免费观看| 国产日韩久久免费影院| 精品亚洲一区二区| 老子影院午夜伦不卡亚洲| 亚洲精品成人区在线观看| 成人性做爰aaa片免费看| 亚洲精品中文字幕无码AV| 无码国产精品久久一区免费| 色偷偷亚洲男人天堂| 国产成人亚洲影院在线观看| 久久综合九色综合97免费下载| 亚洲无砖砖区免费| 国产免费人成视频在线观看 | 亚洲GV天堂GV无码男同| mm1313亚洲国产精品美女| 亚洲国产日韩在线| 国产精品四虎在线观看免费| h视频在线免费观看| 亚洲无成人网77777| 无码不卡亚洲成?人片| 成全视频免费观看在线看| 亚洲av无码电影网| 亚洲毛片网址在线观看中文字幕| 一级毛片在线观看免费| 日韩欧美亚洲国产精品字幕久久久| 亚洲女同成人AⅤ人片在线观看| 最近2019年免费中文字幕高清|