6. Event processing.
The next step is to write an event processor program to respond to component events (such as, select a multi-selection option or click a button to submit a form, etc.). For simple applications, you also need to specify which page will be accessed when a form is submitted or accessed. You can implement this interface for ApplicationHandler to achieve this. The following code segment shows an example. In this example, I have to see if the formit button is triggered in index.jsp. If so, the component tree ID is set to the ID of the component tree associated with the Index.jsp page.
Code 5: BasicApplicationHandler.java
Import java.util.sortedMap;
Import javax.faces.facesexception;
Import javax.faces.component.uicomponent;
Import javax.faces.context.facescontext;
Import javax.faces.tree.tree;
Import javax.faces.tree.treefactory;
Import javax.faces.factoryfinder;
Import javax.faces.lifecycle.ApplicationHandler;
Import javax.faces.event.formevent;
Import javax.faces.event.facesevent;
Import javax.faces.event.commandevent;
Import com.sun.faces.riconstants;
Public Class BasicApplicationHandler Implements ApplicationHandler {
Public Boolean ProcesseVent (FacesContext Context, FaceSevent Facesevent) {
IF (! (! (! (! (! "&& formvent &&
! (Facesevent InstanceOf Commandevent)) {
Return True;
}
Boolean ReturnValue = FALSE;
String TreeId = NULL;
FaceSevent InstanceOf formevent {
ForMevent ForMevent = (ForMevent) Facesevent;
IF (ForMevent.getCommandName (). Equals ("Submit")) {
TreeID = "/Hello.jsp";
}
ReturnValue = True;
} else if (FaceSevent InstanceOf CommandEvent) {
CommandEvent CommandEvent = (CommandEvent) Facesevent;
UIComponent C = CommandEvent.getComponent ();
IF (C.GetaTRibute ("Target")! = null) {
TreeId = (string) C.GetaTribute ("Target");
ReturnValue = True;
}
}
IF (null! = treid) {
TreeFactory TreeFactory = (TreeFactory)
FactoryFinder.getFactory (FactoryFinder.tree_Factory);
Context.Settree (TreeFactory.gettree (Context, TreeID);
Return ReturnValue;
}
}
7. Develop the context monitoring program.
If you read the deployment description file web.xml, you will notice that I declare a servlet context monitor program (BasicServletContextListener). When the application is started, the servlet container creates an instance of servlet context listener and calls its conteitialized method; when the app is closed, its ContexTdStroyed method is called. Here is an example of a servlet context listener.
Code 6: BasicServletContextListener.java
Import Javax.Servlet.ServletContextListener;
Import javax.servlet.servletContextevent;
Import javax.faces.factoryfinder;
Import javax.faces.lifecycle.lifecyclefactory;
Import javax.faces.lifecycle.lifecycle;
Import javax.faces.lifecycle.ApplicationHandler;
Public Class BasicServletContextListener IMPLEments ServletContextListener {
Public BasicServletContextListener () {
}
Public void contextinitialized (servletContextevent E) {
ApplicationHandler Handler = New BasicApplicationHandler ();
LifecycleFactory Factory = (LifecycleFactory)
FactoryFinder.GetFactory (FactoryFinder.lifecycle_Factory);
Lifecycle Lifecycle =
Factory.getlifecycle (lifecyclefactory.default_lifecycle);
Lifecycle.SetApplicationHandler (HANDLER);
}
Public void contextdestroyed (servletContext E) {
}
}
8, complete the response page.
When the form in Index.jsp is submitted, the application processor program is started and the user will be forwarded to the response page Hello.jsp. code show as below:
Code Sample 7: Hello.jsp
<% @ Taglib Uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<% @ Taglib URI = "http://java.sun.com/jsf/core" prefix = "f"%>
h: form>
f: USE_FACES>
Html>
For the rest: http://www.9cbs.net/develop/read_article.asp?id=18705 Develop web applications with JavaServer Faces (1)
Http://www.9cbs.net/develop/read_article.asp?id=18707 Develop web applications with JavaServer Faces (2)
Http://www.9cbs.net/develop/read_article.asp?id=18709 Develop web applications with JavaServer Faces (3) http://www.9cbs.net/develop/read_article.asp?id=18712 with JavaServer Faces Development web application (5)