0

For example in PrimeFaces there is

RequestContext.getCurrentInstance().execute("alert(123)")

And in OmniFaces there is

Ajax.onComplete("alert(123)") 

I tried the pure JSF options I got from:

but neither of them works with Richfaces, specifically I'm either getting no alert in the response (using the first two options), or (using the last option) only the alert and an otherwise empty response with the follwing error in the browser console:

JSF AJAX Error:
description: "An empty response was received from the server. Check server error logs.",
responseCode: 200,
responseText: "<?xml version='1.0' encoding='UTF-8'?>?<partial-response><changes><eval><![CDATA[alert(123);]]></eva??l>",
responseXML: null

How can I just pass some JavaScript to the browser from Java code using RichFaces?

Thanks

1
  • What version of JSF are you using? Depending on the version, you can take advantage of newer response writing libs and just write your script directly to the ajax response Commented Apr 2, 2015 at 16:16

1 Answer 1

1

Do you need to directly call JavaScript from a bean?

RichFaces components have @oncomplete, it lets you set code to be executed when the request comes back from the server.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but yeah I really need this here from within a controller, not component specific. Isn't there some equivalent to RequestContext#getCurrentInstance()#execute() or some hack?
Well, the controller is still responding to a request from the page, isn't it? Anyway concerning the "empty response" error, that's not from RichFaces, by the looks of it the response you're creating is invalid XML. (</eva??l>, doesn't look good.) I've tried it and it works fine.
Oh can you please show me the code you were using? I tried the code suggested in stackoverflow.com/questions/17864326/… which uses eval and caused the invalid response
I tried it again with the quotes fixed, but when I keep the writer.endDocument(); writer.flush(); I don't get the entire response as expected by RichFaces (I don't execute this on a h:commandButton click but in the controller code during another request) and when I omit the writer.endDocument(); writer.flush(); I doesn't get insertert correctly into <partialresponse> tag but just prepended before the <?xml .. start of the response. In both cases I'm getting an erroneous response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.