struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Albert Wong" <>
Subject RE: Setting focus on field with error
Date Thu, 16 Aug 2001 21:04:22 GMT
Struts' html:form tag takes in an optional "focus" property.  If
specified, Struts will generate the neccessary javascript to set the
focus on the specified element.

So, one possibility might be to add a focus attribute to your formbeans
which would allow you to do the following:

<html:form name="FORM_NAME" focus="<%= FORM_NAME.getFocus() %>"
type="com.myCompany.myApp.action.form.MyForm" method="POST"

Adding the focus property can still be considered consistent with the
responsibilty of the form bean -- to represent html form input, we're
just expanding it to include setting up that form too (in this case
which html input gets the focus on that particular form). 

Another way to look at it is, if the jsp page needs to know which form
input needs the focus, which object has that information?  In this case,
Actionform seems like the best candidate.  It knows which input faile
validation and therefore needs to have the focus set on it.

-----Original Message-----
From: Assenza, Chris []
Sent: Thursday, August 16, 2001 1:21 PM
To: ''
Subject: RE: Setting focus on field with error

I've just been asked to do the same exact thing in our app (what a
coincidence!!) but I haven't determined exactly what do yet.  In our
ActionErrors are added by the name of the form field that had the error
field1 is the ActionErrors property and also the html:text property on

I'm thinking that we can expose the ActionErrors object to the front-end
use the messages HashMap or methods inherited from ActionMessages to
identify a property and then use JavaScript to set the focus on that
property.  This can probably be done pretty cleanly with a taglib.  

Again, this is just speculation on my part and I need to do a lot of
research into ActionErrors and Messages because I haven't dug into the
much for them, having had no need to until now. Let me know what you
out! :)


Christopher Assenza
Phone:	412.201.6026
Fax:     412.201.6060
Moving Your Business from Point A to Point e.SM

-----Original Message-----
From: Troy Hart []
Sent: Thursday, August 16, 2001 3:56 PM
Subject: Re: Setting focus on field with error

I could be wrong but I can't think of anything that would be very
The only way I could imagine doing it (and I'm not sure it would even
would be to invoke a javascript function every time the page loads. You
add a bean with the data needed by the javascript  to the request, from

action class. NOTE: this would require you to forgo the automatic form 
validation and instead trigger validation from your action class. This
only way to get the bean I talked about into the request (as a request 
attribute that is).


On Thursday 16 August 2001 11:03 am, you wrote:
> Is their a nifty way to have the focus automagically go to the field
> the error, on an input form?  For example, if I have a form with an
> 'amount' field, and the user enters an invalid amount, causing my
> return an appropriate error, keyed in the ActionErrors container with
> 'amount' property, is there a slick way to have the focus set to that
> on the form?  I know how to have the error message display next to the
> offending field, but having the focus go there also would be nice.
> Thanks!
> Steve Molitor

View raw message