用JavaServer Faces开发Web应用(4)

类别:Java 点击:0 评论:0 推荐:

    6、事件处理。

 

下一步就是写事件处理器程序用来响应组件事件(诸如,选择了一个多选的选项或者点击一个按钮提交一个表单等等)。对于简单的应用,你还需指明当一个表单被提交或者访问一个超级连接时,哪一个页面会被访问。你可以实现ApplicationHandler 这个接口达到这个目的。下面的代码段显示了一个例子。这个例子中,我要看看FormEvent是不是index.jsp中Submit按钮触发的。如果是, 组件树ID就会设置为与index.jsp页面相关联的组件树的ID。

 

代码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 (!(facesEvent instanceof FormEvent) &&

            !(facesEvent instanceof CommandEvent)) {

      return true;

  }

 

        boolean returnValue = false;

        String treeId = null;

 

        if (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.getAttribute("target") != null) {

                treeId = (String)c.getAttribute("target");

                returnValue = true;

            }

        }

 

        if (null != treeId) {

            TreeFactory treeFactory = (TreeFactory)

            FactoryFinder.getFactory(FactoryFinder.TREE_FACTORY);

            context.setTree(treeFactory.getTree(context,treeId));

 

        }

 

        return returnValue;

    }

}

 

 

    7、开发上下文监听程序。

 

如果你仔细看过部署描述文件web.xml,你会注意我声明了一个servlet上下文监听程序(BasicServletContextListener)。当应用启动时,servlet容器会创建一个servlet context listener的实例,并调用它的contextInitialized 方法;当应用关闭的时候,会调用它的contextDestroyed 方法。下面是一个servlet context listener的例子。

 

代码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(ServletContextEvent e){

   }

}

 

 

    8、完成响应页面。

 

当index.jsp中的表单被提交后,应用处理器程序启动,然后用户会被转发到响应页面hello.jsp。代码如下:

 

Code Sample 7: hello.jsp

 

<HTML>

    <HEAD> <title>Hello</title> </HEAD>

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    <body bgcolor="white">

    <f:use_faces>

    <h:form id="responseform" formName="responseform">

    <h2>Hello, <h:output_text id="userLabel"

                    modelReference="UserNameBean.userName" /> </h2>

         <p>

    </h:form>

    </f:use_faces>

</HTML>

 

 

<未完待续>

其余部分请参考:

http://www.csdn.net/develop/read_article.asp?id=18705 用JavaServer Faces开发Web应用(1)

http://www.csdn.net/develop/read_article.asp?id=18707 用JavaServer Faces开发Web应用(2)

http://www.csdn.net/develop/read_article.asp?id=18709 用JavaServer Faces开发Web应用(3)

http://www.csdn.net/develop/read_article.asp?id=18712 用JavaServer Faces开发Web应用(5)

本文地址:http://com.8s8s.com/it/it17750.htm