[EMAIL PROTECTED] 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