Editing and Refactoring Web Application Files


The IDE's Source Editor provides many features that make the typing and changing of code for various web application components easier. This section goes over a few of the features that are particularly useful for web applications. See Chapter 5 for more information on IDE editing and refactoring features.

Completing Tags

The IDE enables you to have tags and tag attributes completed automatically in JSP, HTML, and XML files. The tag completion feature not only reduces the number of keystrokes that you type, but also provides popup documentation for the possible ways to complete the tag or attribute that you are typing, as shown in Figure 8-4.

Figure 8-4. The code completion feature in a JSP file


Open the tag completion popup by typing the beginning of a tag and pressing Ctrl-spacebar or waiting a second for code completion to kick in automatically. If there is only one way to complete the word you are typing, the end of the word is filled in automatically. If there are multiple ways to complete the tag, a popup list of those possibilities is displayed. You can keep typing to narrow the list or select the text you want, using the mouse or arrow keys.

For example, if you want to add the following statement in a JSP file

<jsp:useBean  scope="page"  />


you can enter it in the following steps (don't worrythe number of steps illustrated here may seem daunting, but it is only because they are presented in such minute detail):

1.

Type <jsp:u

2.

Press Ctrl-spacebar (seBean is appended).

3.

Press the spacebar and type i

4.

Press Ctrl-spacebar (d="" is appended with the insertion point left between the quotation marks).

5.

Type hello

6.

Press the right-arrow key and then the spacebar.

7.

Type s

8.

Press Enter (cope="" is appended with the insertion point left between the quotation marks).

9.

Type p

10.

Press Ctrl-spacebar (age is appended).

11.

Press the right-arrow key and then the spacebar.

12.

Type c

13.

Press Enter (lass="" is appended with the insertion point left between the quotation marks).

14.

Type o

15.

Press Ctrl-spacebar (rg is appended, assuming that the class is part of your project).

16.

Type a period (.) and press Ctrl-spacebar (mydomain is filled in).

17.

Type a period (.) and press Ctrl-spacebar (mypackage is filled in).

18.

Type a period (.) and press Ctrl-spacebar (MyClass is filled in).

19.

Press the right-arrow key and then the spacebar and type />

See Generating Code Snippets in Chapter 5 for information on completing Java expressions and configuring code completion.

Using Code Templates for JSP Files

For commonly used JSP code snippets, you can take advantage of code templates in the Source Editor to reduce the number of keystrokes. You can access a JSP code template by typing the abbreviation for the code template and then pressing the spacebar.

See Table 8-3 for a list of code templates for JSP files. Code templates are available for other types of files as well. See Chapter 5, Table 5-1, for a list of code templates for Java classes.

Table 8-3. JSP Abbreviations in the Source Editor

Abbreviation

Expands To

ag

application.getAttribute("|")

ap

application.putAttribute("|")

ar

application.removeAttribute("|")

cfgi

config.getInitParameter("|")

oup

out.print("|")

oupl

out.println("|")

pcg

pageContext.getAttribute("|")

pcgn

pageContext.getAttributeNamesInScope(|)

pcgs

pageContext.getAttributesScope("|")

pcr

pageContext.removeAttribute("|")

pcs

pageContext.setAttribute("|",)

rg

request.getParameter("|")

sg

session.getAttribute("|")

sp

session.putAttribute("|")

sr

session.removeAttribute("|")

jspf

<jsp:forward page="|"/>

jspg

<jsp:getProperty name="|" property=""/>

jspi

<jsp:include page="|"/>

jspp

<jsp:plugin type="|" code="" codebase=""> </jsp:plugin>

jsps

<jsp:setProperty name="|" property=""/>

jspu

<jsp:useBean type=""/>

pg

<%@page |%>

pga

<%@page autoFlush="false"%>

pgb

<%@page buffer="|kb"%>

pgc

<%@page contentType="|"%>

pgerr

<%@page errorPage="|"%>

pgex

<%@page extends="|"%>

pgie

<%@page isErrorPage="true"%>

pgim

<%@page import="|"%>

pgin

<%@page info="|"%>

pgit

<%@page isThreadSafe="false"%>

pgl

<%@page language="java"%>

pgs

<%@page session="false"%>

tglb

<%@taglib uri="|"%>


If an abbreviation for a code template is the same as text that you want to type (for example, you do not want it to be expanded into something else), press Shift-spacebar to keep the text from expanding into the code template.

You can modify the list of code templates in the Options window. See Adding, Changing, and Removing Code Templates in Chapter 5.

Editing the Deployment Descriptor Manually

Although the IDE guides you through the adding of entries for the deployment descriptor (web.xml file) as you add servlets, filters, and listeners to your project, you might have occasion to edit the file by hand.

To open the deployment descriptor in the Source Editor, open the Projects window, expand the Configuration Files node (or the Web Pages | Web-INF node), and double-click the web.xml file. The file opens as a multitab document in the Source Editor with the General tab open (as shown in Figure 8-5). You can edit different parts of the web.xml file with the visual editors for different elements of the file (General, Servlets, Filters, and Pages), or you can click the XML tab to edit the file's XML source directly (see Figure 8-6).

Figure 8-5. Deployment descriptor visual editor


Figure 8-6. Deployment descriptor XML editor


Refactoring Web Components

NetBeans IDE's refactoring support extends to web applications and enterprise applications. For example, you can do the following:

  • Rename classes, methods, and fields of servlets, tag handlers, and other web components. See Renaming All Occurrences of the Currently Selected Class, Method, or Field Name in Chapter 5.

  • Move classes to a different package or project (resulting in the class' being packaged in a different WAR file). See Moving a Class to a Different Package in Chapter 5.

  • Change method parameters, including parameter names, parameter types, and method visibility. You can also add method parameters. See Changing a Method's Signature in Chapter 5.

  • Changing visibility of fields and adding getter and setter accessor methods. See Encapsulating a Field in Chapter 5.

When you rename a web component class (such as a servlet or tag handler), any corresponding name entries in the application's deployment descriptor (web.xml file) and/or tag library descriptor (TLD) are updated as well. When you move a class and the deployment descriptor is affected, you are prompted with a Confirm Changes dialog box to make sure that you want to process changes to the deployment descriptor.

Be careful not to rename or change the parameters of a servlet method that must be implemented with a given name according to the Servlet or Enterprise JavaBeans specifications.




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net