• No results found

In-line code

In document HAHTsite IDE Programming Guide (Page 34-38)

The ability to intermix Java code with other elements (such as text, pictures, widgets, and form elements) gives you a powerful tool for dynamically

• to create “internationalized” pages that contain text in different languages from which a user can select

• to retrieve values passed on the URL from a calling page • to override the value of a form field at runtime

In-line code is typically used for short expressions and statements. To

maintain the WYSIWYG look of your pages, you’ll probably want to minimize the amount of code that you place directly onto a page. You can do this by creating classes and methods in a project’s source pages and calling them from your Web pages. With this approach, you can also share classes and methods among the project’s Web pages.

In this example, a data agent (represented by the icon at the top of the page) populates a form with fields from a database. However, between the data agent and the form, note the code that disables the Next button if we’ve reached end of file.

Besides adding entire lines of code, you can also add expressions. An expression can include variables, constants, operators, and methods, as long as it resolves to a single value. Here are two examples:

a * b / c

aRequest.getURLField("txtCity")

To add a Java expression to an HTML page

1 On the page, click where you want the expression to appear. 2 Click the Server-side Expression button .

3 Type the expression.

4 By default, expressions are displayed in red in the editor, although you can change this setting using Tools > Options.

To add a statement to an HTML page

1 On the page, click where you want the statement to appear. 2 Click the Server-side Statement button .

3 Type one or more statements.

4 By default, statements are displayed in blue in the HTML editor. To convert existing text to an expression or statement

1 Select the text you want to convert.

2 Click either the Server-side Expression button or the Server-side Statement button . Or choose Edit > Character, click the Script tab, and then select the type of expression or statement you want to create.

To format the output of an expression or statement

You can format the output of a Java expression or statement, just as you can format text that you type directly on the page.

1 Select the paragraph or characters you want to affect.

2 To modify the paragraph properties, choose Edit > Paragraph... or make a selection from the Paragraph Styles toolbar.

3 To modify the character properties, choose Edit > Character.... or make a selection from the Character Styles toolbar. (If you modify the font color, the new color will be displayed in the browser, but not in the IDE.)

Invoking an expression or a method from a dialog box

The second way you can customize your project is through dialog boxes. From

Dialog box expressions

To substitute an expression for a field name or constant, prefix the expression with “==”. For example, in the properties for a link, you could define the query string to contain the entry point name of the current page:

callingPage=/*==getName()*/

On the destination page, you could retrieve the information this way: aRequest.getURLField("callingPage")

which would return packagename.class.method, like this: JavaExample.HsLinkStart.run

Notice in the example the use of "/*" and "*/" bracketing the expression. You must use this construction when you want to use an expression in a

name/value attribute pair, or when it appears in the middle of a script. However, if you simply want to set an attribute to the value of an expression, you can use ==expression.

Dialog box method calls

In addition to using expressions, you can call user-defined methods from several property sheets in the IDE, such as the Form Properties dialog and the Link Properties dialog. For example:

• A Java method can generate the binary data to display a picture, an image map, or a multimedia object.

• It can be the destination for a dynamic link (the value of an HREF attribute tag).

• It can be the default action that takes place when the user submits a form, or the action tied to one of the form’s buttons.

Obviously, the content of your Java method will be determined primarily by what you want it to do, and the HAHTsite IDE and IP User’s Guide explains any restrictions on calling a method or subroutine in the documentation for each set of properties. However, if the effect is to display a page, the class should implement the HAHTPage interface, including the run method:

void run (Request aRequest, Response aResponse)

The run method and its arguments are described later in this chapter; see “What’s in a Java page?” on page 18.

In document HAHTsite IDE Programming Guide (Page 34-38)