• No results found

Basic Setup: advanced.jsp(Covered in Previous Lecture)

N/A
N/A
Protected

Academic year: 2019

Share "Basic Setup: advanced.jsp(Covered in Previous Lecture)"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

© 2009 Marty Hall

The AjaxTags Library:

The AjaxTags Library:

Advanced Features

Originals of Slides and Source Code for Examples:

http://courses.coreservlets.com/Course-Materials/ajax.html

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

© 2009 Marty Hall

For live Ajax & GWT training, see training

t htt //

l t

/

courses at http://courses.coreservlets.com/.

Taught by the author of

Core Servlets and JSP

,

More

Servlets and JSP

and this tutorial Available at public

Servlets and JSP

, and this tutorial. Available at public

venues, or customized versions can be held on-site at

your organization.

C d l d d t ht b M t H ll

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

•Courses developed and taught by Marty Hall

–Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF 1.x& 2.0, Ajax, GWT, custom mix of topics

–Ajax courses can concentrate on one library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo) or survey several

•Courses developed and taught by coreservlets.com experts (edited by Marty)

–Spring, Hibernate/JPA, EJB3, Ruby/Rails

(2)

Topics in This Section

Regions that display temporarily while

id

server-side resource runs

Autocomplete textboxes with associated

values displayed in other textboxes

values displayed in other textboxes

Prefunctions and postfunctions

Arbitrary JavaScript that runs before or after server side

Arbitrary JavaScript that runs before or after server-side

resource

Functions that run when server has error

Multiple triggers for server-side resources

4

© 2009 Marty Hall

Setup/Review

(3)

Installation

Several required JAR files

In Eclipse: WebContent/WEB-INF/lib

In deployed app: WEB-INF/lib

Download from http://ajaxtags sourceforge net/

Download from http://ajaxtags.sourceforge.net/

Prototype and Scriptaculous JS files

In Eclipse: WebContent/scripts

In Eclipse: WebContent/scripts

In deployed app: scripts

Download from http://script.aculo.us/

Simple installation

Download prebuilt starting-point project:

j t

bl k i

ajaxtags-blank.zip

Download from

http://courses.coreservlets.com/Course-Materials/ajax.html

6

Basic Setup: advanced.jsp

(Covered in Previous Lecture)

(Covered in Previous Lecture)

<!DOCTYPE ...>

<%@ taglib uri="http://ajaxtags org/tags/ajax" <%@ taglib uri http://ajaxtags.org/tags/ajax

prefix="ajax" %>

<html xmlns="http://www.w3.org/1999/xhtml"> <head>

<meta http-equiv="Content-Type"

content="text/html;charset=UTF-8"" />

<jsp:include page="/WEB-INF/includes/header.jsp"/>

...

<title>Advanced AjaxTags Examples</title> </head>

<b d > <body> ...

(4)

Basic Setup: header.jsp

(Covered in Previous Lecture)

(Covered in Previous Lecture)

<% request.setAttribute("contextPath",

request getContextPath()); %> request.getContextPath()); %>

<script src="${contextPath}/scripts/prototype-1.4.0.js" type="text/javascript"></script>

<script src="${contextPath}/scripts/scriptaculous.js"p { } p p j type="text/javascript"></script>

<script src="${contextPath}/scripts/overlibmws.js" type="text/javascript"></script>

<script src="${contextPath}/scripts/ajaxtags-1.2-beta2.js" type="text/javascript"></script>

<link rel="stylesheet"

h f "${ t tP th}/ / t l "

href="${contextPath}/css/styles.css" type="text/css"/>

8

© 2009 Marty Hall

ajax:autocomplete with

ajax:autocomplete with

indicator

(5)

Basic Idea

Approach

indicator attribute designates section (usually div or span)

that will be enabled while results are being loaded

It is disabled to start with and disabled again when results

It is disabled to start with, and disabled again when results

come back

Often contains animated GIF showing progress

Main use

Main use

Gives user feedback when server-side resource takes a

long time

g

Note

You can reproduce indicator region in other places by

using Effect.Show from Scriptaculous

See later lectures on Scriptaculous

10

JSP Example

<fieldset>

<legend>ajax:autocomplete with indicator</legend>g j p / g <form>

<label for="language">Programming language:</label> <input type="text" name="language" id="language"/>

<span id "indicatorRegion" style "display:none;"> <span id="indicatorRegion" style="display:none;">

<img src="${contextPath}/images/busy-indicator.gif"/> Loading...

</span>

</form>

<ajax:autocomplete source="language" target="language" target= language

baseUrl="${contextPath}/slow-language-completer.ajax" className="autocomplete"

minimumCharacters="1"

(6)

Server-Side Code

public class SlowLanguageCompleter extends LanguageCompleter {

LanguageCompleter shown in previous

extends LanguageCompleter { @Override

public String getXmlContent(HttpServletRequest request, HttpServletResponse

section

HttpServletResponse response)

throws Exception {

try { Thread.y { sleepp(5000); } catch(Exception e) {}( ) } ( p ) {}

return(super.getXmlContent(request, response)); }

}

12

(7)

© 2009 Marty Hall

ajax:autocomplete with

ajax:autocomplete with

Linked Fields

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Basic Idea

Approach

S

id

li

i h

h

Server-side resource returns a list with names that are

different from values

xmlBuilder.addItem("name1", "value1");

I l t

ti

d

l

th

In last section, names and values were the same

– When name selected from dropdown, that name (value) put into the textfield that has the dropdown

Names shown in dropdown box under main textfield

Names shown in dropdown box under main textfield

When a value is selected from dropdown, secondary

value placed into a different textfield

M i

Main uses

When you have auxiliary information to display to user

E.g., show description when catalog ID selected

g ,

p

g

When you have associated values

(8)

JSP Example

<fieldset>

<legend>ajax:autocomplete with linked fields</legend> <legend>ajax:autocomplete with linked fields</legend> <form>

<label for="englishAnimal">English animal:</label> <input type="text" id="englishAnimal"/>

<input type text id englishAnimal />

<label for="spanishAnimal">Spanish animal:</label> <input type="text" id="spanishAnimal"/>

</form>/

<ajax:autocomplete

source="englishAnimal" target="spanishAnimal"

baseUrl="${contextPath}/animal-completer.ajax" parameters="englishAnimal={englishAnimal}" className="autocomplete"

minimumCharacters="1"/> </fieldset>

16

Server-Side Code

import javax.servlet.http.*; import org ajaxtags helpers *; import org.ajaxtags.helpers. ; import org.ajaxtags.servlets.*;

public class AnimalCompleter extends BaseAjaxServlet {

p p j

@Override

public String getXmlContent(HttpServletRequest request, HttpServletResponse response) throws Exception {

String animalPrefix =

request.getParameter("englishAnimal");

St i i lLi t k A i lLi t( i lP fi )

String animalList = makeAnimalList(animalPrefix); return(animalList);

(9)

Server-Side Code (Continued)

private static final String[][] animals =

{ {"aardvark", "aardvark"},{"alligator", "cocodrilo"}, { { aardvark , aardvark },{ alligator , cocodrilo },

{"antelope", "antílopo"}, {"baboon", "babuino"},

{"bat", "murciélago"}, {"bear", "oso"}, {"cat", "gato"}, {"camel", "camello"}, {"conch", "concha"}

};

private String makeAnimalList(String animalPrefix) { animalPrefix = animalPrefix.toUpperCase();

AjaxXmlBuilder builder = new AjaxXmlBuilder(); AjaxXmlBuilder builder = new AjaxXmlBuilder(); for(String[] animalPair: animals) {

String englishAnimal = animalPair[0]; String spanishAnimal = animalPair[1];

if(englishAnimal.toUpperCase().startsWith(animalPrefix)) { builder.addItem(englishAnimal, spanishAnimal);

} } }

return(builder.toString()); }

}

18

(10)

© 2009 Marty Hall

ajax:updateField with

ajax:updateField with

postFunction

p

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Basic Idea

Approach

Y

li

l J

S i f

i

h

l

f h

You list a regular JavaScript function as the value of the

postFunction or preFunction attribute

Almost all tags support postFunction and preFunction

The postFunction fires after the server-side resource is

finished

The preFunction fires just before the server-side resource

p

j

is called

Main uses

preFunction

preFunction

To make an “I am working” message or image

Form field validation

postFunction

postFunction

(11)

Script

scripts/postFunction.js

N

h i i

i

i

l

ll

Note that it is using scriptaculous calls

function showConversionSuccess() {

// Temporarily highlight Celsius and Kelvin fields

// p y g g

new Effect.Highlight('c'); new Effect.Highlight('k');

// Turn on successMessage div, then fade it out. Element.show('successMessage');( g );

setTimeout("Effect.DropOut('successMessage');", 2000); }

advanced.jsp

Loads scriptaculous and Prototype.

Defines contextPath variable. <head>

...

<jsp:include page="/WEB-INF/includes/header.jsp"/> <script src="${contextPath}/scripts/postFunction.js"p ${ }/ p /p j

type="text/javascript"></script> <title>Advanced AjaxTags Examples</title> </head>

22

JSP Example

<fieldset>

<legend>ajax:updateField with postFunction</legend>g j p p / g <form>

<label for="f">Enter temperature in Fahrenheit:</label> <input type="text" id="f"/>

<input type "button" id "convertButton" value "Convert"/> <input type="button" id="convertButton" value="Convert"/> <hr width="500" align="left"/>

<label for="c">Temperature in Celsius:</label> <input type="text" id="c"/>

<label for="k">Temperature in Kelvin:</label> <input type="text" id="k"/><p/>

<div id="successMessage" style="display:none" class="temporaryMessage">

(12)

JSP Example (Continued)

<ajax:updateField source="f"

source= f target="c,k"

baseUrl="${contextPath}/temperature-converter.ajax" action="convertButton"

action= convertButton parameters="f={f}"

parser="new ResponseXmlParser()"

postFunction="showConversionSuccess"/>

postFunction showConversionSuccess /> </fieldset>

24

Server-Side Code

Same TemperatureConverter as in previous

i

section

Given a temperature in Fahrenheit, returns a list of

corresponding temperatures in Celsius and Kelvin

corresponding temperatures in Celsius and Kelvin

(13)

Style Sheet Entries

.temporaryMessage {

border:2px solid blue; border:2px solid blue; background-color:#eeffee; color:#009900;

font weight: bold; font-weight: bold; }

26

Results

Immediately after pressing button.

(14)

© 2009 Marty Hall

ajax:tabPanel with

ajax:tabPanel with

errorFunction

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Basic Idea

Approach

You list a regular JavaScript function as the value of the

errorFunction attribute

Almost all AjaxTags tags support errorFunction

Almost all AjaxTags tags support errorFunction

If server-side resource returns an HTTP status code other

than 200, the errorFunction is triggered

Main uses

Error messages

Default values when server side resource is unavailable

(15)

Script

scripts/errorFunction.js

function warn() {

alert("Server error!\n\n" +

"Did you forget to specify\n" + " d f lt t b?")

"a default tab?"); }

error.jsp

<head> ... ... <jsp:include page="/WEB-INF/includes/header.jsp"/> <script src="${contextPath}/scripts/errorFunction.js" type="text/javascript"></script>

<title>Advanced AjaxTags Examples</title> </head>

30

JSP Example

<fieldset>

<legend>ajax:tabPanel</legend>

<h2>Largest Cities in Selected Northeast States</h2> <div class="tabPanelWrapper"> <ajax:tabPanel panelStyleId="panel" contentStyleId="content" panelStyleClass="tabPanel" contentStyleClass="tabContent" currentStyleClass="currentTab"

Should say defaultTab="true". Without a default tab, tabPanel tries to load contextPath/null.

(16)

Server-Side Code

Same PopulationFinder as in previous

i

section

Given a state and a city, returns the population

Use of errorFunction does not change how server side

Use of errorFunction does not change how server-side

resource works

32

(17)

© 2009 Marty Hall

ajax:htmlContent with

ajax:htmlContent with

Multiple Triggers

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Basic Idea

Approach

In previous section, we used

source

to designate button

Gives the ID of the element that will trigger the action

If you use

If you use

sourceClass

sourceClass

instead then any element that

instead, then any element that

has that CSS classname can be a trigger

The element must support onclick (push buttons, links,

radio buttons checkboxes etc )

radio buttons, checkboxes, etc.)

Main use

(18)

JSP Example

<fieldset>

<legend>ajax:htmlContent with Multiple Triggers</legend> <form>

<label for="state2">State:</label> <select id="state2">

<option value="">Select State</option> <option value="Maryland">Maryland</option> ...

<option value="New York">New York</option> </select>

<label for="city2">City:</label>

<select id="city2" disabled="disabled"> <option value="">Select City</option> </select>

<br/><hr align="left" width="500"/>

<input type="button" value="Show Population" class="trigger"/> <a href="javascript://no-op" class="trigger">Show Population</a> <input type="radio" class="trigger"/>Show Population

<br/><hr align="left" width="500"/> <span id="population"></span>

</form>

36

JSP Example (Continued)

<ajax:select

baseUrl="${contextPath}/city-finder.ajax"${ }/ y j source="state2"

target="city2"

parameters="state={state2}"/> <ajax:htmlContent

<ajax:htmlContent

baseUrl="${contextPath}/population-finder.ajax"

sourceClass="trigger"

target="population"

(19)

Server-Side Code

Same PopulationFinder as in previous

l

examples

Given a state and a city, returns the population

Type of trigger does not change how server side resource

Type of trigger does not change how server-side resource

works

38

(20)

Summary

indicator attribute

Specifies region that displays temporarily while server side resource

Specifies region that displays temporarily while server-side resource

runs

Returning a list for ajax:autocomplete with names

different from values

different from values

Names get shown in the dropdown list

Name gets inserted in source textfield

• I e the textfield that actually had dropdown listI.e., the textfield that actually had dropdown list

Value gets inserted in target textfield

preFunction and postFunctions attributes

Arbitrary JavaScript that runs before or after server-side resource

Arbitrary JavaScript that runs before or after server-side resource

errorFunction attribute

Function that runs when server has error

sourceClass attribute

sourceClass attribute

Designates elements that trigger server-side resources

40

© 2009 Marty Hall

Questions?

References

Related documents

If you enter the account number of the vendor here, all payments are made using the bank details of this business partner (bank transfers, automatic debit, credit memos)E. This fi

STEPHANIE: Roger, talk a little bit about how you disseminate the FTI culture to your employees around the world, particularly those that are new to the company and my not

This suggests that despite the presence of bi-directional relationship between the two markets, the causal relationship running from index futures to the component stocks

Bone and Joint Radiology, lecture and lab (current) Advanced Imaging, lecture and lab (previous) Principles of Diagnostic Imaging, lecture (previous) Issues in Chiropractic

Furthermore, we introduce a smoothness constraint by expanding the pixel-wise sparse PST into a joint sparse recovery problem where several adjacent pixels are processed

JSP file and executes the servlet) (HTTP get or post) (HTML File) Response Request Servlet File Response Request Java Component JSP Engine... User request a JSP Execute Servlet

– Data contained in app server’s request, session, application scopes App Server Server-side Code (Java servlets, JSP) Browser http request html response?.

1 Display a strategy page for the controller containing the required node as described in the ‘Go to a Strategy Page’ section of this manual. 2 If the Command Line toolbar is