Ok, for now I've commented out the bit where I detect the parent, until
I can figure a better way to do it, or give up, and remove the comments.
As an effect of the change, you now have to give the full ID, but you
can also put it anywhere in the page.
Here's the code, I'll put it back later tomorrow unless you say
otherwise... And then start with moving it all to the sandbox.
@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
String forID = (String) component.getAttributes().get("for");
ResponseWriter writer = context.getResponseWriter();
// XXX - I'd still like to get the parentID, but need to add a
check if it's a form or not...
//UIComponent parentComponent = component.getParent();
//String parentID = parentComponent.getClientId(context);
//String targetID = parentID+":"+forID;
String targetID = forID;
writer.startElement("script", component);
writer.writeAttribute("type", "text/javascript", null);
writer.writeText("setFocus('",null);
writer.writeText(targetID, null);
writer.writeText("');\n",null);
writer.writeText("function setFocus(elementId) { var element = " +
"document.getElementById(elementId); if (element &&
element.focus) " +
"{ element.focus(); } }",null);
writer.endElement("script");
}
Jason Lee wrote:
> On Tue, Apr 8, 2008 at 8:03 PM, Jim Driscoll <Jim.Driscoll_at_sun.com
> <mailto:Jim.Driscoll_at_sun.com>> wrote:
>
> Thanks to Jason and Ryan for the comments. I've incorporated all
> comments (final strings, value expressions, and renderer) except
> the facelets tag descriptor - (I haven't worked with facelets
> before, and would like to put this back soon, rather than take a
> couple days to learn facelets. Hope that's ok. I need to also add
> facelet tags for the two validators I've written as well.)
>
>
> I think this code may be problematic:
>
> UIComponent parentComponent = component.getParent();
> String parentID = parentComponent.getClientId(context);
> String targetID = parentID+":"+forID;
>
> That assumes that the component's parent is the form, but it may not
> be. While it may not make sense to nest this in something other than
> h:form, someone's bound to do it, and the "failure" of the component
> won't have an obvious cause.
>
> I think it would be better, also, to add a type="text/javascript"
> attribute to the tag. In the JS, you call the method before you declare
> it. Does that work? Doesn't seem like it would, but JS engines are
> kinda strange, so I could be wrong. :P
>
> --
> Jason Lee, SCJP
> Software Architect -- Objectstream, Inc.
> Mojarra and Mojarra Scales Dev Team
> https://mojarra.dev.java.net
> https://scales.dev.java.net
> http://blogs.steeplesoft.com