cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joerg Heinicke <joerg.heini...@gmx.de>
Subject Re: How to access cocoon object from javascript scriptAction?
Date Wed, 10 Oct 2007 12:29:03 GMT
Instead of cocoon.getComponent() you have to use manager.lookup(). Look 
at ServiceManager [1] for the details. It's available in your JavaScript 
as you can see in ScriptAction's code.

Joerg

[1] 
http://excalibur.apache.org/apidocs/org/apache/avalon/framework/service/ServiceManager.html

On 10.10.2007 7:44 Uhr, Josh2007 wrote:
> Thanks for the information,
> 
> I took a look at ScriptAction's source code.
> I have a better understanding of how it works but it seems I need to import
> in my scriptAction other components and functions (DOMParser,
> session.setAttribute...).
> 
> Actually I'm trying to adapt my flowscript code to a scriptAction as I need
> to stay in the same sitemap after the execution of the javascript.
> 
> Here's the flowscript I'm trying to adapt.
> 
> // loadDocument() reads in an XML file and returns a DOM Document. 
> function loadDocument(uri) {
> var parser = null;
> var source = null;
> var resolver = null;
> try {
> parser =
> cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
> resolver =
> cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
> source = resolver.resolveURI(uri);
> var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
> is.setSystemId(source.getURI());
> return parser.parseDocument(is);
> } finally {
> if (source != null) resolver.release(source);
> if (parser != null) cocoon.releaseComponent(parser);
> if (resolver != null) cocoon.releaseComponent(resolver);
> }
> }
> 
> // ------------------------- getStreamToSession
> -------------------------------------------
> 
> // retrieve the XML
> var stream = loadDocument("cocoon:/stream");
> 
> // save stream in session
> var session = cocoon.session;
> session.setAttribute("stream", stream);
> 
> //
> ---------------------------------------------------------------------------------------------
> 
> Of course, getComponent to load java components is undefined as I don't have
> access to the class, the same with the parser.
> 
> I noticed that getComponent is part of org.apache.components.flow and
> therefore not accessible from scriptAction.
> 
> I should be able to import java comonents with BSF. 
> 
> Do you have any idea how to do it?
> 
> Thanks,
> 
> Josh
> 
> 
> Joerg Heinicke wrote:
>> On 09.10.2007 17:13 Uhr, Josh2007 wrote:
>>
>>> I need to access cocoon object from a javascript scriptAction to make use
>>> of
>>> session, request... functions.
>> The ScriptAction works more like a usual Cocoon Java component than like 
>> flowscript. Therefore you have the ObjectModel available which is 
>> actually only a Map. To access the stuff you need you use the 
>> ObjectModelHelper and call the corresponding static methods like 
>> getRequest() or getSession() [1]. If you have a look into ScriptAction's 
>> source code (which is simple) you will see the other objects it 
>> registers for usage in the JavaScript:
>>
>> mgr.registerBean("resolver", resolver);
>> mgr.registerBean("objectModel", objectModel);
>> mgr.registerBean("parameters", par);
>>
>> // ScriptAction housekeeping
>> mgr.registerBean("actionMap", actionMap);
>>
>> // helpers
>> mgr.registerBean("logger", getLogger());
>> mgr.registerBean("request", ObjectModelHelper.getRequest(objectModel) );
>> mgr.registerBean("scriptaction", this );
>> mgr.registerBean("manager", this.manager );
>>
>>
>>> It seems the only way to access cocoon object is to create a flowscript.
>>> Is there another solution as I don't want to redirect to another pipeline
>>> after the script is performed unlike flowscript does?
>> The cocoon object is only a simplified version to access all the stuff 
>> in the ObjectModel. It's indeed only available in flow script.
>>
>> Joerg
>>
>> [1] 
>> http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/environment/ObjectModelHelper.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message