眉標=Java #副標=JavaServer Faces程式設計 #大標=進階流程控制與JSF內建展示套件入門(下) #作者=文/王森 ============================================ age.jsp <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 歡迎光臨 請輸入姓名 :
請輸入年齡(0-100) :


================================= 最後我們撰寫事件處理函式,其輪廓大致上如下: ================================= ================== MyActionListener.java package my ; import javax.faces.application.* ; import javax.faces.event.* ; import javax.faces.el.*; import javax.faces.context.*; import javax.faces.*; public class MyActionListener implements ActionListener { public PhaseId getPhaseId() { return PhaseId.UPDATE_MODEL_VALUES; } public void processAction(ActionEvent event) { } } ================================= ================================= MyActionListener.java … public class MyActionListener implements ActionListener { … public void processAction(ActionEvent event) { String actionCommand = event.getActionCommand(); FacesContext context = FacesContext.getCurrentInstance(); ValueBinding name = getValueBinding("mydbean1.name") ; ValueBinding message = getValueBinding("mydbean1.message") ; String nametmp = "" ; String tmp="" ; try { nametmp = (String)name.getValue(context); if(nametmp!=null) nametmp = new String(nametmp.getBytes("ISO-8859-1"),"Big5") ; else nametmp = "無名式" ; tmp = "按下的命令為:"+actionCommand+";"+"使用者姓名:"+ nametmp ; tmp = new String(tmp.getBytes("Big5"),"ISO-8859-1") ; }catch(Exception e){} message.setValue(context,tmp) ; } } ================================= =================================== 請輸入姓名 : : =================================== =================================== =================================== =================================== MyValueChangedListener.java package my ; import javax.faces.application.* ; import javax.faces.event.* ; import javax.faces.el.*; import javax.faces.context.*; import javax.faces.*; public class MyValueChangedListener implements ValueChangedListener { public PhaseId getPhaseId() { return PhaseId.UPDATE_MODEL_VALUES; } public void processValueChanged(ValueChangedEvent event) { } } =================================== ===================================    MyValueChangedListener.java … … public class MyValueChangedListener implements ValueChangedListener { … … public void processValueChanged(ValueChangedEvent event) { String componentId = event.getComponent().getComponentId(); String tmp = "無改變" ; if (componentId.equals("name")) { tmp = "名字改變了" ; }else if(componentId.equals("age")) { tmp = "年齡改變了" ; } try { tmp = new String(tmp.getBytes("Big5"),"ISO-8859-1") ; }catch(Exception e){} ValueBinding message = getValueBinding("mydbean1.message") ; FacesContext context = FacesContext.getCurrentInstance(); message.setValue(context,tmp) ; } … … } =================================== =================================== ActionListenerImpl.java public class ActionListenerImpl implements ActionListener { public ActionListenerImpl() { } public PhaseId getPhaseId() { return PhaseId.INVOKE_APPLICATION; } public void processAction(ActionEvent event) { javax.faces.component.UIComponent source =            event.getComponent(); UICommand command = (UICommand)source; FacesContext context = FacesContext.getCurrentInstance(); ApplicationFactory aFactory = (ApplicationFactory)FactoryFinder.getFactory("javax.faces.application.ApplicationFactory"); Application application = aFactory.getApplication(); String outcome = null; outcome = command.getAction(); String actionRef = null; ValueBinding binding = null; Object action = null; if(null == outcome) { actionRef = command.getActionRef(); if(actionRef != null) { binding = application.getValueBinding(actionRef); if(binding != null) try { action = binding.getValue(context); } catch(PropertyNotFoundException e) { } if(null == action || !(action instanceof Action)) { Object obj[] = new Object[1]; obj[0] = actionRef; throw new IllegalArgumentException(Util.getExceptionMessage("com.sun.faces.NO_ACTION_FROM_ACTIONREF", obj)); } outcome = ((Action)action).invoke(); } } NavigationHandler navHandler = application.getNavigationHandler(); navHandler.handleNavigation(context, actionRef, outcome); } } =================================== =================================== public PhaseId getPhaseId() {   return PhaseId.INVOKE_APPLICATION; } =================================== =================================== NavigationHandlerImpl.java public class NavigationHandlerImpl extends NavigationHandler { ……. public void handleNavigation(FacesContext context, String actionRef, String outcome) { String newTreeId = getTreeId(context, actionRef, outcome); if(newTreeId != null) { TreeFactory treeFactory = (TreeFactory)FactoryFinder.getFactory("javax.faces.tree.TreeFactory"); Assert.assert_it(null != treeFactory); context.setTree(treeFactory.getTree(context, newTreeId)); } } …… } ===================================