我已经设置了一个简单的maven项目,其中包含以下依赖项:
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.1</version>
</dependency>
</dependencies>
这页:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:body onkeydown="alert('You pressed some key!')">
<h:outputLabel value="Hello, young fellows!"/>
</h:body>
</html>
这是生成的html,注意没有方法的body标签:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><body><label>Hello, young fellows!</label></body>
</html>
我尝试通过更改pom.xml中的依赖项版本将primefaces版本回滚到4.0,如下所示:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
然后它按预期工作:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><body onkeydown="alert('You pressed some key!')"><label>Hello, young fellows!</label></body>
</html>
有什么问题?这是PF 5.1或其他什么的错误吗?
答案 0 :(得分:6)
从5.0开始,h:body
在PrimeFaces中有renderer,BodyRenderer
。
在该渲染器的encodeBegin
中,传递attributes数组,HTML.BODY_ATTRS
。
public static final String[] BODY_ATTRS = {
"dir",
"lang",
"style",
"onload",
"onunload"
};
正如您所看到的,由于某种原因,此数组并未涵盖h:body
标记的所有实际属性,因此会忽略某些属性。
为了避免这个问题,你可以简单地将该渲染器扩展为一个接受所有实际属性的渲染器。
public class CustomBodyRenderer extends BodyRenderer{
//our array with all the attributes of h:body tag
public static final String[] BODY_ATTRS = {
"dir",
"lang",
"onclick",
"ondblclick",
"onkeydown",
"onkeypress",
"onkeyup",
"onmousedown",
"onmousemove",
"onmouseout",
"onmouseover",
"onmouseup",
"style",
"title",
"onload",
"onunload"
};
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = component.getClientId(context);
writer.startElement("body", component);
if (shouldWriteId(component)) {
writer.writeAttribute("id", clientId, "id");
}
String styleClass = (String) component.getAttributes().get("styleClass");
if (styleClass != null && styleClass.length() != 0) {
writer.writeAttribute("class", styleClass, "styleClass");
}
//the only changed line from the original renderer
renderPassThruAttributes(context, component, BODY_ATTRS);
}
}
然后在 faces-config
中注册<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>javax.faces.Body</renderer-type>
<renderer-class>com.hatemalimam.CustomBodyRenderer</renderer-class>
</renderer>
</render-kit>
通过这种方式,您可以在“最小”变化中获得预期结果。
我已在跟踪器上提交了issue。
<强>更新强> 这已在PF 5.2中修复。