我已经设置了一个具有以下依赖项的简单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>
Run Code Online (Sandbox Code Playgroud)
而这个页面:
<?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>
Run Code Online (Sandbox Code Playgroud)
这是生成的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>
Run Code Online (Sandbox Code Playgroud)
我尝试通过更改pom.xml中的依赖项版本将primefaces版本回滚到4.0,如下所示:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后它按预期工作:
<?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>
Run Code Online (Sandbox Code Playgroud)
问题是什么?这是PF 5.1或其他什么的错误吗?
从5.0开始,在PrimeFaces中h:body有一个渲染器BodyRenderer.
在该encodeBegin渲染器中,传递了一系列属性HTML.BODY_ATTRS.
public static final String[] BODY_ATTRS = {
"dir",
"lang",
"style",
"onload",
"onunload"
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,由于某种原因,此数组不会涵盖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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在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>
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以在"最小"变化中获得预期结果.
我已在跟踪器上提交了一个问题.
更新: 这已在PF 5.2中修复.
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |