You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by sh...@sms.at on 2005/09/13 13:54:16 UTC

PhaseListener

Hello!

I have created a own custom component and made jar file. But if I use it in another project, my java files could not find the images and javascripts.

I heard from the PhaseLIstener. But I don't know how to use and whether I should use it or not.

Does someone know a good example or can me tell what I have to do?

Thanks
shed

--
Ein Service von http://www.sms.at


Re: PhaseListener

Posted by Werner Punz <we...@gmx.at>.
shed@sms.at wrote:
> Hello!
> 
> I have created a own custom component and made jar file. But if I use it in another project, my java files could not find the images and javascripts.
> 
> I heard from the PhaseLIstener. But I don't know how to use and whether I should use it or not.
> 
> Does someone know a good example or can me tell what I have to do?
> 
> Thanks
> shed
> 
> --
> Ein Service von http://www.sms.at
> 
> 
Phase listeners are classes which are event listeners on the different 
phases.
They are called centrally and have to filter their trigger criterion 
themselves.

You sort of can see them as central event listeners on the JSF Rendering 
cycle.

Phase listeners are very helpful if you want to add some servlet like
functionality without having to define a servlet.
For instance you can use them to trigger a javascript rendering and 
serve the javascript from inside a jar that way.

public class RenderScriptPhaseListener implements PhaseListener {
	

public PhaseId getPhaseId() {
		return PhaseId.RESTORE_VIEW;
	}
public void renderScript(PhaseEvent event, String resourceLocation) {

		InputStream stream = null;
		BufferedReader bufReader = null;
		HttpServletResponse response = (HttpServletResponse) 
event.getFacesContext().getExternalContext().getResponse();
		OutputStreamWriter outWriter = null;
		String curLine = null;

		try {
			outWriter = new OutputStreamWriter(response.getOutputStream(), 
response.getCharacterEncoding());
			stream = new 
BufferedInputStream(RenderScriptPhaseListener.class.getResourceAsStream(resourceLocation));
			bufReader = new BufferedReader(new InputStreamReader(stream));			
			while (null != (curLine = bufReader.readLine())) {
				outWriter.write(curLine + "\n");
			}
			outWriter.flush();
	
			outWriter.close();
			event.getFacesContext().responseComplete();
		} catch (Exception e) {
			String message = "Can't load script file:" + resourceLocation;
			System.err.println(message);
			e.printStackTrace();
		}
	}


public void afterPhase(PhaseEvent event) {
		String rootId = event.getFacesContext().getViewRoot().getViewId();
	
		if (rootId.endsWith(ScriptController.FAT_VIEW_ID)) {
			handleFatEvent(event);

		}
	}

	void handleFatEvent(PhaseEvent event) {
		renderScript(event, ScriptController.SCRIPT_RESOURCE_FAT);
	}

}

ublic class ScriptController {
	public static final String	SCRIPT_RESOURCE_FAT			= "/somepath/fat.js";

...


This should give you the idea the main points are:

the main point is


public PhaseId getPhaseId() {
		return PhaseId.RESTORE_VIEW;
	}

this one tells the that the listener is only allowed to trigger at the 
restore view phase

ublic void afterPhase(PhaseEvent event) {
		String rootId = event.getFacesContext().getViewRoot().getViewId();
	
		if (rootId.endsWith(ScriptController.FAT_VIEW_ID)) {
			handleFatEvent(event);

		}
	}

and that one is called after the phase... there the script is rendered 
with standard java and httprequest mechanisms


Re: PhaseListener

Posted by Matthias Wessendorf <mw...@gmail.com>.
Inside of MyFaces' sandbox stuff there are some ajax components.
For these components there is a PhaseListener to handle the AJAX Request 
inside of the "JSF Engine"

Also it is possible to use a PhaseListener for providing JS files or images 
of your custom components

And even for debugin (or learning the JSF lifecycle ;)
intersting blog entrie for PhaseListeners: --> 
http://jroller.com/page/cschalk?entry=getting_familiar_with_the_jsf

-Matthias

On 9/13/05, shed@sms.at <sh...@sms.at> wrote:
> 
> Hello!
> 
> I have created a own custom component and made jar file. But if I use it 
> in another project, my java files could not find the images and javascripts.
> 
> I heard from the PhaseLIstener. But I don't know how to use and whether I 
> should use it or not.
> 
> Does someone know a good example or can me tell what I have to do?
> 
> Thanks
> shed
> 
> --
> Ein Service von http://www.sms.at
> 
> 


-- 
Matthias Wessendorf
Zülpicher Wall 12, 239
50674 Köln