For example, traditional GUI toolkits provide layout managers, in one form or another, that allow layout algorithms to be encapsulated and reused. This article explores a template mechanism for Java. Server Pages (JSP) that, like layout managers, encapsulates layout so it can be reused instead of replicated. Because layout undergoes many changes over the course of development, it's important to encapsulate that functionality so it can be modified with minimal impact to the rest of the application. In fact, layout managers demonstrate an example of one of the tenets of object- oriented design: encapsulate the concept that varies, which is also a fundamental theme for many design patterns. JSP does not provide direct support for encapsulating layout, so Webpages with identical formats usually replicate layout code; for example, Figure 1 shows a Webpage containing header, footer, sidebar, and main content sections. Figure 1. Webpage layout. The layout of the page shown in Figure 1 is implemented with HTML table tags: Example 1. Including content< html> < head> < title> JSP Templates< /title> < /head>. In the example listed above, content is included with the JSP include directive, which allows the content of the page to vary - - by changing the included files - - without modifying the page itself. However, because layout is hard coded, layout changes require modifications to the page. If a Website has multiple pages with identical formats, which is common, even simple layout changes require modifications to all of the pages. To minimize the impact of layout changes, we need a mechanism for including layout in addition to content; that way, both layout and content can vary without modifying files that use them. That mechanism is JSP templates. Using templates. Templates are JSP files that include parameterized content. The templates discussed in this article are implemented with a set of custom tags: template: get, template: put, and template: insert. The template: get tag accesses parameterized content, as illustrated in Example 2. Webpages with the format shown in Figure 1. Example 2. a. A template< %@ taglib uri='/WEB- INF/tlds/template. Example 2. a is nearly identical to Example 1, except we use template: get instead of the include directive. Let's examine how template: get works. Java bean with the specified name from request scope. The bean contains the URI (Uniform Resource Identifier) of a Web component that's included by template: get. For example, in the template listed in Example 2. URI - - header. html - - from a bean named header in request scope. Subsequently, template: get includes header. The template is included with template: insert. Example 2. b illustrates the use of the put and insert tags: Example 2. Using the template from Example 2. WEB- INF/tlds/template. Template. jsp'>. Templates' direct='true'/>. The insert start tag specifies the template to be included, in this case the template listed in Example 2. Build a Rich Client Platform To-Do Application in NetBeans IDE. Practice using NetBeans IDE features that improve code quality and increase developer productivity. Learn how to use Netbeans IDE Keyboard Shortcuts using Code Template. Advantage of using netbeans. Suppose you print something on screen then you can use SOUT and tab. Orelse click on help and. Use JSP templates to encapsulate Webpage layout and encourage modular design. Code Templates in NetBeans IDE for PHP. Code templates are prewritten snippets of code provided by NetBeans IDE. You can paste a snippet into your code by using code completion or the template. Each put tag stores a bean in request scope and the insert end tag includes the template. The template subsequently accesses the beans as described above. A direct attribute can be specified for template: put; if direct is set to true, the content associated with the tag is not included by template: get, but is printed directly to the implicit out variable. Displaying Line Numbers. By default, line numbers are switched off in the Source Editor to save space and reduce visual clutter. If you need the line numbers, you can turn them on by choosing View .In Example 2. b, for example, the title content - - JSP Templates - - is used for the window title. Websites containing multiple pages with identical formats have one template, such as the one listed in Example 2. JSP pages, such as Example 2. If the format is modified, changes are restricted to the template. Another benefit of templates and including content in general is modular design. For example, the JSP file listed in Example 2. Example 2. c. Example 2. Also, although header. HTML file, it does not contain the usual preamble of HTML tags such as < html> or < body> because those tags are defined by the template. That is, because the template includes header. Note: JSP provides two ways to include content: statically, with the include directive, and dynamically, with the include action. The include directive includes the source of the target page at compile time and is equivalent to C's #include or Java's import. Sometimes you may not remember how an items starts, making it difficult to use code completion. Instead, to see all items that relate to listening to property changes, you can. Thanks Josch for adding your favorite shortcut. Let other readers also add their favorite shortcuts in comments and this will be a list of most favorite shortcuts in NetBeans IDE with regards Tushar. The include action includes the target's response generated at runtime. Like the JSP include action, templates include content dynamically. So, although the JSP pages in Example 1 and Example 2. Optional content. All template content is optional, which makes a single template useful to more Webpages. For example, Figure 2. Figure 2. b show two pages - - login and inventory - - that use the same template. Both pages have a header, footer, and main content. The inventory page has an edit panel (which the login page lacks) for making inventory changes. Figure 2. a. A login form. Figure 2. b. An inventory page. Below, you'll find the template shared by the login and inventory pages: < %@ taglib uri='template. For example, the same JSP template, which includes the edit panel only when the user's role is curator, produces the two pages shown in Figures 3. Figure 3. a. Inventory page for curators. Figure 3. b. Inventory page for other users. The template used in Figures 3. Let's look at how the tag handler for template: get uses the role attribute: public class Get. Tag extends Tag. Support . Each insert start tag creates a hashtable and pushes it on the stack. The enclosed put tags create beans and store them in the newly created hashtable. Subsequently, get tags in the included template access the beans in the hashtable. Figure 4 shows how the stack is maintained for nested templates. Figure 4. Storing template parameters in request scope. Each template in Figure 4 accesses the correct footer; footer. If the beans were stored directly in request scope, step 5 in Figure 4 would overwrite the footer bean specified in step 2. Template tag implementations. The remainder of this article examines the implementation of the three template tags: insert, put, and get. We begin with sequence diagrams, starting with Figure 5. It illustrates the sequence of events for the insert and put tags when a template is used. Figure 5. Sequence diagrams for the put and insert tags. If a template stack does not already exist, the insert start tag creates one and places it in request scope. A hashtable is subsequently created and pushed on the stack. Each put start tag creates a Page. Parameter bean, stored in the hashtable created by the enclosing insert tag. The insert end tag includes the template. The template uses get tags to access the beans created by put tags. After the template is processed, the hashtable created by the insert start tag is popped off the stack. Figure 6 shows the sequence diagram for template: get. Figure 6. Sequence diagrams for the get tag. Template tag listings. Tag handler implementations for the template tags prove straightforward. Example 3. a lists the Insert. Tag class - - the tag handler for template: insert. These detailed instructions are for users of the Net. Beans IDE. The Net. Beans IDE runs on the Java platform, which means that you can use it with any operating system for which there is a JDK available. These operating systems include Microsoft Windows, Solaris OS, Linux, and Mac OS X. A Checklist. To write your first program, you'll need: The Java SE Development Kit (JDK 7 has been selected in this example)The Net. Beans IDECreating Your First Application. Your first application, Hello. World. App, will simply display the greeting . Using IDE projects eliminates configuration issues normally associated with developing on the command line. You can build or run your application by choosing a single menu item within the IDE. Add code to the generated source file. A source file contains code, written in the Java programming language, that you and other programmers can understand. As part of creating an IDE project, a skeleton source file will be automatically generated. You will then modify the source file to add the . The instructions contained within this file are known as bytecodes. Run the program. The IDE invokes the Java application launcher tool (java), which uses the Java virtual machine to run your application. Create an IDE Project. To create an IDE project: Launch the Net. Beans IDE. On Microsoft Windows systems, you can use the Net. Beans IDE item in the Start menu. On Solaris OS and Linux systems, you execute the IDE launcher script by navigating to the IDE's bin directory and typing ./netbeans. On Mac OS X systems, click the Net. Beans IDE application icon. In the Net. Beans IDE, choose File . You should see the following components: The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on. The Source Editor window with a file called Hello. World. App. java open. The Navigator window, which you can use to quickly navigate between elements within the selected class. Net. Beans IDE with the Hello. World. App project open. Add JDK 8 to the Platform List (if necessary)It may be necessary to add JDK 8 to the IDE's list of available platforms. To do this, choose Tools . You should now see this newly added platform: The Java Platform Manager. To set this JDK as the default for all projects, you can run the IDE with the - -jdkhome switch on the command line, or by entering the path to the JDK in the netbeans. You should see a screen similar to the following: The IDE is now configured for JDK 8. Add Code to the Generated Source File. When you created this project, you left the Create Main Class checkbox selected in the New Project wizard. The IDE has therefore created a skeleton class for you. Both the compiler (javac) and launcher (java) are case- sensitive, so you must capitalize consistently. Hello. World. App is not the same as helloworldapp. You have successfully compiled your program! If the build output concludes with the statement BUILD FAILED, you probably have a syntax error in your code. Errors are reported in the Output window as hyperlinked text. You double- click such a hyperlink to navigate to the source of an error. You can then fix the error and once again choose Run . You can see where the new file is generated by opening the Files window and expanding the Hello World App/build/classes/helloworldapp node as shown in the following figure. Files window, showing the generated . Now that you have built the project, you can run your program. Run the Program. From the IDE's menu bar, choose Run ! Your program works! Continuing the Tutorial with the Net. Beans IDEThe next few pages of the tutorial will explain the code in this simple application. After that, the lessons go deeper into core language features and provide many more examples. Although the rest of the tutorial does not give specific instructions about using the Net. Beans IDE, you can easily use the IDE to write and run the sample code. The following are some tips on using the IDE and explanations of some IDE behavior that you are likely to see: Once you have created a project in the IDE, you can add files to the project using the New File wizard. If you use the Run Main Project command, the IDE will run the file that the IDE associates as the main class of the main project. Therefore, if you create an additional class in your Hello. World. App project and then try to run that file with the Run Main Project command, the IDE will run the Hello. World. App file instead. You might want to create separate IDE projects for sample applications that include more than one source file. As you are typing in the IDE, a code completion box might periodically appear. You can either ignore the code completion box and keep typing, or you can select one of the suggested expressions. If you would prefer not to have the code completion box automatically appear, you can turn off the feature. The IDE prompts you with the Rename dialog box to lead you through the options of renaming the class and the updating of code that refers to that class. Make the changes and click Refactor to apply the changes. This sequence of clicks might seem unnecessary if you have just a single class in your project, but it is very useful when your changes affect other parts of your code in larger projects. For a more thorough guide to the features of the Net. Beans IDE, see the. Net. Beans Documentation page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |