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

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

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

    大漠駝鈴

    置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
    Java,Php,Shell,Python,服務(wù)器運(yùn)維,大數(shù)據(jù),SEO, 網(wǎng)站開發(fā)、運(yùn)維,云服務(wù)技術(shù)支持,IM服務(wù)供應(yīng)商, FreeSwitch搭建,技術(shù)支持等. 技術(shù)討論QQ群:428622099
    隨筆 - 238, 文章 - 3, 評(píng)論 - 117, 引用 - 0
    數(shù)據(jù)加載中……

    A Blog Application with Warp (continued)(2)

    A Blog Application with Warp (continued)

    First check out the previous tutorial on creating a Blog application to get yourself going. In this article, I'll show you how to add forms and a bit of interactivity. You will learn the following Warp concepts:

    • Event Handling and Navigation
    • Events and the Button component
    • The TextField and TextArea components
    • Page scopes

    Continuing from the previous tutorial, let's now make a "compose new entry" page. This will be very similar to the other pages with some different components:

    <html>

    <head w:component="meta">
    <title>Warp :: Compose New Blog Entry</title>
    </head>

      <body w:component="frame">

    <input w:component="textfield" w:bind="newBlog.subject" />
    <input w:component="textarea" w:bind="newBlog.text" />

    <input w:component="button" w:label="post blog" />
    </body>

    </html> 

    The first important part to notice is that the <head> tag is decorated with a Meta component. This is very important indeed--along with the Frame component on <body>, Meta forms the foundation for Warp page behavior.

    The other things to notice are the input components. TextField is a simple text box, we use the attribute w:bind to tell Warp where to bind the user input to. In this case I am giving it a path to a property of newBlog, which is a variable in my page object. OK, let's create ourselves this property:

    @URIMapping("/blogs/compose")
    public class ComposeBlog {
    private Blog newBlog = new Blog("", ""); //an empty blog

    @OnEvent
    public void save() {
    System.out.println(newBlog.getSubject() + " - "

    + newBlog.getText());
    }
    public Blog getNewBlog() {
    return newBlog;
    }
    }

    Here I have provided an event handler method named save(). By tagging it with the @OnEvent annotation, I have told Warp to invoke this method whenever the page triggers an event (in our case, the clicking of the button). First, the data is synchronized between the input components (TextField and TextArea) and the bound properties, then the event handler is fired which prints out their content.

    Let's add some more functionality, where our list of blogs (from the previous tutorial) actually gets updated. For this we first need to add a method in ListBlogs that stores a new blog entry into the map. That part is easy enough:

    @URIMapping("/home")
    public class ListBlogs {
    private Map<String, Blog> blogs = new HashMap<String, Blog>();

    public ListBlogs() { .. }

    public void addNewBlog(Blog blog) {
    blogs.put(blog.getSubject(), blog);
    }

    //...

     Ok now let us invoke the store method from our compose page's event handler (remember Page-injection from the previous tutorial):

    @URIMapping("/blogs/compose")
    public class ComposeBlog {
    private Blog newBlog = new Blog("", ""); //an empty blog

    @Inject @Page ListBlogs listBlogs;

    @OnEvent
    public void save() {
    listBlogs.addNewBlog(newBlog);
    }

    public Blog getNewBlog() {
    return newBlog;
    }
    }

    We're almost there, now when I save the entry, I want it to come back to the blog list. This follows the post-and-redirect design pattern common in web development. Warp supports this in an intuitive, type-safe manner. After saving the blog in my event handler, I simply return the page object that I want shown:

        @OnEvent
    public ListBlogs save() {
    listBlogs.addNewBlog(newBlog);

    return listBlogs;

    }

    Neat! Now when you click the "post blog" button, it runs the save() method and redirects you to back to the blog list. You can return any type of object from an event handler so long as it is a page object (or a subclass of one). You can also redirect to an arbitrary URL or use JSP-style forwarding instead of post-and-redirect. Check out the Event Handling guide on the wiki for details.

    One last step concerning Page scoping. Typically, Warp obtains an instance of a page object from the Guice injector on every request. This effectively means that any page object that is not bound (see Guice user guide for information on scopes) with a given scope is instantiated once per request. Since our HashMap is a property of the ListBlogs page, this means that when we redirect (in a new request), the Map gets blown away and we lose our new entry.

    To fix this, we can scope the ListBlogs page object as a singleton. This means that ListBlogs is only created once for the lifetime of the webapp, and the Map of entries is retained.

    @URIMapping("/home")
    @Singleton
    public class ListBlogs { .. }

    You should be careful about relying on page objects to maintain state and do a lot of thinking before declaring a scope on a page.

    The singleton scope is an easy fix for our example but in the real world you will want to store the blogs in a more permanent storage medium (such as a database or persistent store). In my next tutorial, we'll see how to do just that using JPA and Hibernate.

     

    posted on 2009-02-17 16:30 草原上的駱駝 閱讀(213) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA框架

    主站蜘蛛池模板: 亚洲av日韩片在线观看| 亚洲欧洲另类春色校园小说| 亚洲天堂在线播放| 国产精品亚洲综合一区在线观看| 久久国产精品免费视频| 亚洲国产精品人人做人人爽| 亚洲a∨国产av综合av下载| 九九九精品成人免费视频| 亚洲人成依人成综合网| 免费网站看v片在线香蕉| 亚洲色大成网站www永久| 三年片在线观看免费观看大全动漫| 久久久久久精品免费免费自慰| 亚洲欭美日韩颜射在线二| 国产成人综合亚洲| 亚洲成人精品久久| 国产2021精品视频免费播放| 免费在线观看一区| 亚洲精品偷拍视频免费观看| 精品无码专区亚洲| 亚洲国产香蕉人人爽成AV片久久 | 97久久免费视频| 亚洲欧洲精品在线| 免费精品国产自产拍在线观看图片| 亚洲国产中文在线视频| 18禁男女爽爽爽午夜网站免费| 亚洲1234区乱码| 成人片黄网站色大片免费| 午夜亚洲国产精品福利| 久久久无码精品亚洲日韩京东传媒 | 亚洲人成在久久综合网站| 中文亚洲AV片不卡在线观看| 青柠影视在线观看免费| 久久精品国产亚洲夜色AV网站| 最近中文字幕高清免费中文字幕mv| 免费视频成人国产精品网站| 亚洲AV永久无码精品一福利 | 久久久久久久尹人综合网亚洲 | 亚洲日韩欧洲乱码AV夜夜摸| 国产美女精品久久久久久久免费 | 免费av欧美国产在钱|