publicvoidaddNavigationCase(ConfigNavigationCase navigationCase){//获取fromViewId标签值String fromViewId = navigationCase.getFromViewId();//获取<navigation-case>标签下值List<ConfigNavigationCase> caseList = caseListMap.get(fromViewId);//caseListMap获取不到fromViewIdif(caseList ==null){//初始化caseListMap的Value值
caseList =newArrayList<ConfigNavigationCase>();//将ConfigNavigationCase对象添加caseList
caseList.add(navigationCase);//将fromViewId和caseList存放入caseListMap
caseListMap.put(fromViewId, caseList);}//caseListMap获取fromViewId,对fromViewId重复处理else{String key = navigationCase.getKey();boolean foundIt =false;for(int i =0; i < caseList.size(); i++){ConfigNavigationCase navCase = caseList.get(i);// if there already is a case existing for the// fromviewid/fromaction.fromoutcome combination,// replace it ... (last one wins).//if(key.equals(navCase.getKey())){
caseList.set(i, navigationCase);
foundIt =true;break;}}if(!foundIt){
caseList.add(navigationCase);}}//如果fromViewId是以*结尾//将fromViewId存放入wildcardMatchListif(fromViewId.endsWith("*")){
fromViewId =
fromViewId.substring(0, fromViewId.lastIndexOf('*'));
wildcardMatchList.add(fromViewId);}}
3.3 获取caseListMap
/**
* Return a <code>Map</code> of navigation mappings loaded from
* the configuration system. The key for the returned <code>Map</code>
* is <code>from-view-id</code>, and the value is a <code>List</code>
* of navigation cases.
*
* @return Map the map of navigation mappings.
*/publicMap<String,List<ConfigNavigationCase>>getNavigationCaseListMappings(){if(caseListMap ==null){returnCollections.emptyMap();}return caseListMap;}
3.4 获取wildcardMatchList
/**
* Return all navigation mappings whose <code>from-view-id</code>
* contained a trailing "*".
*
* @return <code>TreeSet</code> The navigation mappings sorted in
* descending order.
*/publicTreeSet<String>getNavigationWildCardList(){return wildcardMatchList;}
3.5 SortIt内部类
/**
* This Comparator class will help sort the <code>ConfigNavigationCase</code> objects
* based on their <code>fromViewId</code> properties in descending order -
* largest string to smallest string.
*/staticclassSortItimplementsComparator<String>{publicintcompare(String fromViewId1,String fromViewId2){return-(fromViewId1.compareTo(fromViewId2));}}
//fromAction为跳转画面Action接口名//outcome为跳转下一个画面keypublicvoidhandleNavigation(FacesContext context,String fromAction,String outcome){if(context ==null){String message =MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,"context");thrownewNullPointerException(message);}//outcome等于null,直接跳过if(outcome ==null){return;// Explicitly remain on the current view}//获取视图对象CaseStructCaseStruct caseStruct =getViewId(context, fromAction, outcome);ExternalContext extContext = context.getExternalContext();if(caseStruct !=null){ViewHandler viewHandler =Util.getViewHandler(context);assert(null!= viewHandler);if(caseStruct.navCase.hasRedirect()){// perform a 302 redirect.String newPath =
viewHandler.getActionURL(context, caseStruct.viewId);try{// encode the redirect to ensure session state// is maintained
extContext.redirect(extContext.encodeActionURL(newPath));}catch(java.io.IOException ioe){thrownewFacesException(ioe.getMessage(), ioe);}
context.responseComplete();}else{UIViewRoot newRoot = viewHandler.createView(context,
caseStruct.viewId);
context.setViewRoot(newRoot);}}}
5.3获取视图对象CaseStruct,Method「getViewId」
privateCaseStructgetViewId(FacesContext context,String fromAction,String outcome){//获取FacesContext上下文根节点viewUIViewRoot root = context.getViewRoot();String viewId =(root !=null? root.getViewId():null);// if viewId is not null, use its value to find// a navigation match, otherwise look for a match// based soley on the fromAction and outcomeCaseStruct caseStruct =null;if(viewId !=null){//根节点viewId,从caseListMap获取List<ConfigNavigationCase>//相当于获取多个<navigation-case>标签
caseStruct =findExactMatch(viewId, fromAction, outcome);if(caseStruct ==null){
caseStruct =findWildCardMatch(viewId, fromAction, outcome);}}if(caseStruct ==null){
caseStruct =findDefaultMatch(fromAction, outcome);}if(caseStruct ==null&& logger.isLoggable(Level.WARNING)){if(fromAction ==null){}else{}}return caseStruct;}