I've added these to our Jyve FAQ:

How Do I use Javascript in my Struts forms?
-------------------------------------------

The relevant HTML tags provide support for the Javascript event handlers
onblur, onchange, onclick, ondblclick, onfocus, onkeydown, onkeypress,
onkeyup, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup.

For more about Javascript events, see the Netscape Javascript Reference
guide <
http://developer.netscape.com/docs/manuals/communicator/jsref/evnt.htm
>.

For more about HTML tags, see the Struts HTML Tag Guide <
http://developer.netscape.com/docs/manuals/communicator/jsref/evnt.htm
>.

The Javascript itself can then be placed 

1. Inline, if it is simple enough, like onclick="history.go(-1)"
2. As a <SCRIPT> in your JSP
3. As an included <SCRIPT src="my.js"> in your JSP

Note that the Struts Javascript properties use all lowercase
identifiers, for XML compatibility. 


How Do I use Javascript to perform Validations in Struts?
---------------------------------------------------------

The best way is using David Winderfeldt's Struts validator servlet
<http://home.earthlink.net/~dwinterfeldt/>. This provides seamless
client-side and server-side validation using regular expressions. 

Other than that, you can also attach other Javascript validation to the
Struts form fields using the provided properties for Javascript event
handling. Of course, these types of validations cannot be guaranteed if
a browser has Javascript turned off.

Why doesn't Struts provide it's own facility for regex or Javascript
validations?

It's on the TODO list for the 1.1 release. 



Is it possible to combine dynamic Javascript menu systems with Struts?
----------------------------------------------------------------------

The challenge with doing this is that the JSP tags run on the server (as
the page is  being generated), while the JavaScript runs on the client
side.  To integrate the two, you need your tags (and other JSP code) to
dynamically generate the JavaScript functions themselves -- sort of
having a program write a program -- so that the JavaScript is customized
to your particular need on this particular page.

A very trivial example is the way that the <html:form> tag deals with
the "focus" attribute.  If you specify it, a dynamically generated bit
of JavaScript is created to set the input focus, which includes the name
of the field you want initial focus 
assigned to.

For a more comprehensive scenario, consider that for most menuing
systems you will need to configure the list of available options into a
JavaScript array or something.  You could use the <logic:iterate> tag to
render the elements that get set in the array's initialization
expression.

[Craig McClanahan]


-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/

Reply via email to