How to define body id attribute in JSF 2?

I need to set the id attribute of html body -tag (for selenium test). But the JSF 2 body tag ( <h:body> ) does not have an id attribute.

So how can I specify the id attribute for the html body in JSF 2?

+2
source share
1 answer

<h:body> really doesn't support it (admittedly, to my surprise, it works great for HTML). I reported this to the JSF guys as issue 2409 .

At the same time, assuming you are using Mojarra, you can solve this by extending the Mojarra BodyRenderer as follows:

 package com.example; import java.io.IOException; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import com.sun.faces.renderkit.html_basic.BodyRenderer; public class BodyWithIdRenderer extends BodyRenderer { @Override public void encodeBegin(FacesContext context, UIComponent component) throws IOException { super.encodeBegin(context, component); if (component.getId() != null) { context.getResponseWriter().writeAttribute("id", component.getClientId(context), "id"); } } } 

To run it, register it in faces-config.xml (no, the @FacesRenderer annotation magic will not work to override the standard renderers).

 <render-kit> <renderer> <component-family>javax.faces.Output</component-family> <renderer-type>javax.faces.Body</renderer-type> <renderer-class>com.example.BodyWithIdRenderer</renderer-class> </renderer> </render-kit> 
+4
source

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


All Articles