[jboss-user] [JBoss Seam] - Re: Referencing global view-id variables from .jpdl.xml

spambob do-not-reply at jboss.com
Fri Dec 15 14:06:14 EST 2006


Thanks Gavin, now it's working like a charm :)

In case anyone is interested my solution is the following:
package com.example.usecases.web;
  | 
  | import java.io.File;
  | import java.util.HashMap;
  | import java.util.Iterator;
  | 
  | import javax.faces.context.FacesContext;
  | import javax.servlet.ServletContext;
  | 
  | import org.dom4j.Document;
  | import org.dom4j.DocumentException;
  | import org.dom4j.Element;
  | import org.dom4j.io.SAXReader;
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.core.Pageflow;
  | import org.jboss.seam.pageflow.Page;
  | 
  | @Name("org.jboss.seam.core.pageflow")
  | public class MyPageflow extends Pageflow {
  | 
  | 	private static final long serialVersionUID = -7905951040207871423L;
  | 	private static HashMap<String, String> viewMap = null;
  | 	
  | 	@In(value = "org.jboss.seam.core.facesContext", required = false)
  | 	private FacesContext facesContext;
  | 
  | 	@Override
  | 	protected String getViewId(Page page) {
  | 		if(!page.getViewId().startsWith("/") && facesContext != null) {
  | 			if(viewMap == null) {
  | 				viewMap = createViewMap(((ServletContext) facesContext.getExternalContext().getContext()).getRealPath(""));
  | 			}
  | 			return viewMap.get(page.getViewId());
  | 		}
  | 		return super.getViewId(page);
  | 	}
  | 	
  | 	private HashMap<String, String> createViewMap(String realPathPrefix) {
  | 		String[] configFiles = {
  | 				"WEB-INF" + File.separator + "faces-config.xml",
  | 				"WEB-INF" + File.separator + "navigation.xml" };
  | 		HashMap<String, String> viewMap = new HashMap<String, String>();
  | 		for (int i = 0; i < configFiles.length; i++) {
  | 			Document doc = null;
  | 			File file = null;
  | 			try {
  | 				file = new File(realPathPrefix + File.separator	+ configFiles[ i ]);
  | 				doc = new SAXReader().read(file);
  | 			} catch (DocumentException e) {
  | 				System.out.println("Can't read navigation rules from file: " + file.getAbsolutePath());
  | 				continue;
  | 			}
  | 			Element rootElement = doc.getRootElement();
  | 			Iterator rootIterator = rootElement.elementIterator("navigation-rule");
  | 			while (rootIterator.hasNext()) {
  | 				Element element = (Element) rootIterator.next();
  | 				if (element.element("from-view-id") == null
  | 						|| element.element("from-view-id").getText().equals("*")) {
  | 					Element navigationCase = element.element("navigation-case");
  | 					viewMap.put(navigationCase.element("from-outcome").getText(),
  | 							navigationCase.element("to-view-id").getText());
  | 				}
  | 			}
  | 		}
  | 		return viewMap;
  | 	}	
  | }

The only drawback is that the path on the filesystem is read from the servletcontext - therefore it doesn't work upon first request.

If someone knows a more elegant solution i would be glad to hear it ;)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3994319#3994319

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3994319



More information about the jboss-user mailing list