2013-08-02 43 views
1

我使用primeface的UI组件,我必须设置布局部临时使用的CSS样式它做的背景下,如何添加CSS样式属性动态地在JSF

.layoutCustomStyle.ui-layout-unit-content 
    { 
    background-image: url('resources/images/backgrnd.png'); 
    } 

的ID该layoutunit的是“layoutId”和所用的styleClass是“layoutCustomStyle”

在XHTML中,

<p:layoutUnit position="top" id= "layoutId" styleClass ="layoutCustomStyle"> 
</p:layoutUnit> 

但我要的是动态地添加背景图像。图像将由文件浏览器选择,所以我不能为此添加单独的类并使用bean。

UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot(); 
UIComponent comp= view.findComponent("layoutId"); 
Map<String, Object> attrMap = comp.getAttributes(); 
String className = (String)attrMap.get("styleClass"); 

使用此我可以设置和获取类名,但如何更改属性“background-image:”动态?

希望这个问题是明确的。任何帮助表示赞赏。

由于提前, 飞马

+0

动态的意思是什么事情你想改变背景??你使用任何Bean类?我告诉我,然后我会为你提供代码。 –

+0

有一个文件浏览器,所选图像应设置为背景。是的,我正在使用bean class.I使用primeface fileupload组件来选择图像文件。 – user2632444

回答

3

使用style属性,而不是styleClass

+0

我动态地添加了样式“background-image:url(”“)”。但是在调用“更新”时,布局单元消失了(它将一些位置靠近标题布局单元并隐藏起来)。它的问题与“更新” – user2632444

+1

只需使用'style =“#{bean.style}”'',而'#{bean}'是一个视图范围的bean。手动抓取组件并以编程方式直接操作它们一直是一种可怕的方式。 – BalusC