开发者

JSF 2 - Unique ids across multiple pages

开发者 https://www.devze.com 2023-04-09 04:35 出处:网络
I\'m looking to create a JSF application in which there are multiple xhtml pages displayed in different regions of the browser. I could do this with iframes but they add extra memory and accessing obj

I'm looking to create a JSF application in which there are multiple xhtml pages displayed in different regions of the browser. I could do this with iframes but they add extra memory and accessing objects across iframes is not that trivial.

I am considering an approach in which I load different xhtml pages within the main page using ajax. The problem is that there will be many elements within the main page that will have the same ids, since the id开发者_如何学编程s are unique only within their respective view roots.

I know 1 solution would be to implement some custom client side logic that handles these duplicate ids, but it would be better to just not have duplicate ids at all.

What can i do to solve the problem?

A side question: Is there a framework that handles such a requirement better, i.e. having multiple pages displayed within the same browser window?


You could replace the view root with one that implements NamingContainer:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <component>
    <component-type>javax.faces.ViewRoot</component-type>
    <component-class>components.ReplacementRoot</component-class>
  </component>
</faces-config>

This sample implementation creates a clientId based on the viewId:

public class ReplacementRoot extends UIViewRoot implements NamingContainer {
  @Override
  public String getClientId(FacesContext context) {
    return "jsf" + getViewId().replaceAll("[^\\p{Alnum}]", "_");
  }
}

This will generate client identifiers of the form jsf_index_xhtml:foo.


Have a look on the Portlet technology since probably this is what you want. The problem is that I haven't seen yet really good portlet implementations, and also that you need a JSF portlet bridge in order to handle the JSF lifecycle - don't know the state of that either.

Another option would be to create your own facelets provider, that adds dynamically whatever components you need, so when you rebuild the component tree, they will suddenly appear. I couldn't tell you more about that either, but I've seen it done and working for some wiki-like software that was editing the pages as facelets xhtml.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号