Automatically generated identifiers for facelets components

We use facelets to create custom ajaxy components. One of the ways that we would like to simulate from the default components is that the identifier is optional, and the identifier is generated if it is not passed. I can already do it as follows:

<ui:composition ...>
  <div class="myComponent" id="#{jsfSupport.generateId(id)}">
     ...
  </div>
</ui:composition>

I use JBoss el to call the support method (you can also use the el functions):

public class JsfSupport {

  public String generateId(String id) {

    if (id==null || "".equals(id){
  return FacesContext.getCurrentInstance().getViewRoot().createUniqueId();
    }
    return id;
  }

}

The problem is that if I need this identifier somewhere in my javascript code in the component, I need to download it again. So I thought I could do the following:

<ui:composition ...>
  <c:set var="id" value="#{jsfSupport.generateId(id)}" />
  <div class="myComponent" id="#{id}">
     ...
  </div>
  <script type="text/javascript">
    document.getElementById('#{id}');
  </script>
</ui:composition>

But that does not work. The identifier is still regenerated, and I get two different ones. Any ideas on what would be the perfect way to do this?

+3
1

<c:set> Facelets aliasing, , JSP. #{id} #{jsfSupport.generateId(id)}, , .

<c:set>, :

public class SetOnceHandler extends TagHandler
{
    private TagAttribute var;
    private TagAttribute value;

    public SetOnceHandler(TagConfig cfg) 
    {
        super(cfg);
        value = getRequiredAttribute("value");
        var = getRequiredAttribute("var");
    }

    public void apply(FaceletContext ctx, UIComponent parent) 
    {
        ctx.setAttribute(var.getValue(ctx), value.getObject(ctx));
    }
}
+1

Source: https://habr.com/ru/post/1783596/


All Articles