Tapestry5之方法返回值

在JumpStart中有相关的介绍,总结一下

void - redisplay the same page.
null - redisplay the same page.
false - same as returning null.

这三个一样,应该没有什么区别。


Class (must be a page class) - go to new page.
String (must be a logical page name) - go to new page.

这俩,基本相同(“PageA”, PageA.java)。


page object - go to new page, with the option of setting up its parameters first.

@InjectPage
private SomePage page;

这样的话,我们可以page.setSomeProperty("someValue");

return page;

即可。

Link - go to new page, with the option of setting up a context first.

    @Inject
    private PageRenderLinkSource pageRenderLinkSource;

    Link onReturnLink() {
        String parameters = "Howdy";
        Link link = pageRenderLinkSource.createPageRenderLinkWithContext(ReturnTypesLink.class, parameters);
        return link;
    }

这个貌似比较牛逼。其实和PageReturn差不大多。主要是这个Inject挺有意思,它有两个返回link的方法,一个是只有一个参数的,一个是带两个参数的,第二个参数就是所有的url参数信息。


StreamResponse - used by pages or components to return images, PDFs, XML, etc.

if (postfix.equals(".XLS") || postfix.equals(".XLT") || postfix.equals(".XLW") || postfix.equals(".CSV")) {
contentType = "application/vnd.ms-excel";
} else if (postfix.equals(".DOC")) {
contentType = "application/msword";
} else if (postfix.equals(".RTF")) {
contentType = "application/rtf";
} else if (postfix.equals(".TEXT")) {
contentType = "text/plain";
} else if (postfix.equals(".XML") || postfix.equals(".TXT")) {
contentType = "";
return;
} else if (postfix.equals(".BMP")) {
contentType = "image/bmp";
} else if (postfix.equals(".JPG") || postfix.equals(".JPEG")) {
contentType = "image/jpeg";
} else if (postfix.equals(".GIF")) {
contentType = "image/gif";
} else if (postfix.equals(".AVI")) {
contentType = "video/x-msvideo";
} else if (postfix.equals(".MP3")) {
contentType = "audio/mpeg";
} else if (postfix.equals(".MPA") || postfix.equals(".MPE") || postfix.equals(".MPEG") || postfix.equals(".MPG")) {
contentType = "video/mpeg";
} else if (postfix.equals(".PPT") || postfix.equals(".PPS")) {
contentType = "application/vnd.ms-powerpoint";
} else if (postfix.equals(".PDF")) {
contentType = "application/pdf";
} else if (postfix.equals(".ZIP") || postfix.equals(".RAR")) {
contentType = "application/zip";
}

以上就是所有的contentType了(几乎哈)。

实现代码基本如下:

    StreamResponse onShowPDFFile()
    {
        return new StreamResponse() {
            InputStream inputStream;
            public void prepareResponse(Response response)
            {
                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                //inputStream = classLoader.getResourceAsStream("company/study/tapestry/pages/doc/Test.txt");
                inputStream = classLoader.getResourceAsStream("company/study/tapestry/pages/doc/DOC.doc");
                try {
                    response.setHeader("Content-Length", "" + inputStream.available());
                } catch (IOException e) {
                    // Ignore the exception in this simple example.
                }
            }
            public InputStream getStream() throws IOException
            {
                return inputStream;
            }
            public String getContentType()
            {
                return "application/msword";
            }
        };
    }

 

URL - go to a java.net.URL of an external page.

    URL onReturnURL() throws MalformedURLException {
        return new URL("http://tapestry.apache.org");
    }

true - redisplay same page, aborting event bubbling (see Event Bubbling example).

这个真心不懂。

------------

如果返回值为true的话,就不会继续查找满足条件的方法了。因为方法可能会出现重载等。

posted @ 2013-03-25 16:55  VoctrALs  阅读(261)  评论(0编辑  收藏  举报