| 3.4.2 page composition with includes |
|
使用一个jsp文件创建完整的显示 (with custom tags and beans to access the required dynamic data) 非常普遍,示例程序就是这样的。然而,许多应用要求在一个页面中显示多个逻辑上不同的部分。 例如,一个入口程序页面可能拥有下面部分或全部的功能: · 访问搜索引擎。 · 一个或多个"news feed"被显示,with the topics of interest customized from the users registration profile. · access to discussion topics related to this portal. · a "mail waiting" indicator if your portal provides free email accounts. 开发不同的部分很容易,你需要将他们分成不同的部分。然后,你可以使用jsp的include 功能来将他们合并成一个单一的页面,或使用struts提供的include tag 。有三种类型的include 可用, depending on when you want the combination of output to occur: · an <%@ include file="xxxxx" %> directive can include a file that contains java code or jsp tags. the code in the included file can even reference variables declared earlier in the outer jsp page. the code is inlined into the other javaserver page before it is compiled so it can definately contain more than just html. · the include action (<jsp:include page="xxxxx" flush="true" />) is processed at request time, and is handled transparently by the server. among other things, that means you can conditionally perform the include by nesting it within a tag like equals by using its parameter attribute. · the bean:include tag takes either a an argument "forward" representing a logical name mapped to the jsp to include, or the "id" argument, which represents a page context string variable to print out to the jsp page. 另外一个方法是使用struts template tag 库。 tiles is an alternative to the original template tag library, offering several enhancements and new capabilities. tiles is available in the nightly build, and from cedric dumoulins web site. |
| 3.4.3 image rendering components |
|
一些应用要求动态的产生图片,如股票的价格走势图。有两种方法可以满足这个需求: · render a hyperlink with a url that executes a servlet request. the servlet will use a graphics library to render the graphical image, set the content type appropriately (such as to image/gif), and send back the bytes of that image to the browser, which will display them just as if it had received a static file. · render the html code necessary to download a java applet that creates the required graph. you can configure the graph by setting appropriate initialization parameters for the applet in the rendered code, or you can have the applet make its own connection to the server to receive these parameters. |
| 3.4.4 rendering text |
一些应用要求动态的产生text or markup, such as xml. if a complete page is being rendered, and can be output using a printwriter, this is very easy to do from an action:
response.setcontenttype("text/plain"); // or text/xml
printwriter writer = response.getwriter(); // use writer to render text return(null); |
