从4.0升级到Primefaces 5.1后不呈现主体属性(onkeydown,onkeyup ...) [英] Body attributes (onkeydown, onkeyup...) not rendered after upgrading to Primefaces 5.1, from 4.0
问题描述
我已经建立了一个具有以下依赖项的简单maven项目:
I have set up a simple maven project with the following dependencies:
<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标签:
This is the generated html, notice the body tag without methods:
<?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,如下所示:
I tried to rollback the primefaces version to 4.0 by changing the dependency version in pom.xml, like this:
<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或其他方面的错误吗?
What is the problem? Is it a bug on PF 5.1 or something else?
推荐答案
从5.0开始,h:body
具有 BodyRenderer
.
Starting from 5.0 the h:body
has a renderer in PrimeFaces, BodyRenderer
.
在该渲染器的encodeBegin
中,一个 HTML.BODY_ATTRS
.
In the encodeBegin
of that renderer an array of attributes is being passed, HTML.BODY_ATTRS
.
public static final String[] BODY_ATTRS = {
"dir",
"lang",
"style",
"onload",
"onunload"
};
正如您所看到的,由于某种原因,此数组不能覆盖h:body
标记的所有实际属性,因此某些属性将被忽略.
And as you can see that this array for some reason doesn't cover all the actual attributes of the h:body
tag, thus some of the attributes are being ignored.
为避免此问题,您可以简单地将该渲染器扩展为可以接受所有实际属性的渲染器.
In order to avoid this problem you can simply extend that renderer into a one which accepts all the actual attributes.
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>
这样,您将获得最小"变化的预期结果.
This way you would get the expected outcome in a "minimal" changes.
我已在跟踪器上提交了问题.
I have submitted an issue on the tracker.
更新: 此问题已在PF 5.2中修复.
这篇关于从4.0升级到Primefaces 5.1后不呈现主体属性(onkeydown,onkeyup ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!