"h:outputText"也可以这样用,如果是碰到JS语句也可以运行比如alert()

(1)
在我的JSF页面中想表达这样的语句:
<h:outputText id="scripthere" escape="false" value="<b>This is a text</b>" />
但是页面报错误:The value of attribute "value" associated with an element type "h:outputText" must not contain the '<' character.
查了下,这个比较好解决,办法如下:
<b><h:outputText id="scripthere" escape="false" value="This is a text" /></b>
(2)但是我要达到这样的效果就难了。
<h:outputText id="scripthere" escape="false" value="<script>alert('提示信息');</script>" />
我也用了上面的那个办法,但是还是报那个错误。至今还没有找到办法....
找到了办法:
<script>
<h:outputText id="scripthere" value="alert('提示信息')"/>
</script>
这样每行每行写就不会全部飘红了。(注意这里的id,其实在后面已经验证了这里不要id)
其实我最终的目的不是这样写的,
最终目的写法:
<script>
<h:outputText id="scripthere" value="#{publishTemplate.script}"/>;
</script>
这样写也没有达到我要的效果,因为页面重新加载一次就使得我private String script;这个为空(这里为空的原因在后面也说了是因为按钮的原因我这里用的是ajax4jsf的,在后面也改成普通的按钮,以便点击后来刷新整个页面)
public String getScript() {
System.out.println("getscript="+script);
return script;
}
public void setScript(String script) {
System.out.println("setscript="+script);
this.script = script;
}
我在判断没有目录的时候会设置它的值:
else{
System.out.println("must be publish template at first !");
this.setScript("alert('提示信息');");
}
但是当页面重新加载一次我就得不到它的值了。所以可能这种方法行不通啦 !!!
最后实在没办法了,就把那个script属性设置成static,这时候就弹出对话框来了,因为很难看它当页面还没有加载完的时候就弹出对话框把整个屏幕都全笼罩了,所以修改template.xhtml里面的那段代码:
<script>
window.onload = new Function("<h:outputText value="#{publishTemplate.script}"/>");
</script>
这样修改就好看些了。
但是当用已经存在的目录来操作时,因为它是static所以达不到效果,所以最后放弃这种方式。
哈哈,上面的情况总得不到值是因为我的按钮的问题,我原来用的是:
<a:commandButton value="Publish" action="#{publishTemplate.publishOneDirectory()}" rendered="#{templateBean.select}" />
xmlns:a="http://richfaces.org/a4j"
这里是ajax4jsf的按钮,a 是ajax的功能 它提交执行后 不再重绘整个页面
,重绘整个页面时它会去bean里取的。所有改成h:commandButton了。这样在后台就可以得到值,但是在前台还是没有得到。待解决中...
最终的解决办法找到啦!!!
完整的代码如下:
(1)类:
private String script;
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
public void publishOneDirectory(){
if(localFilePath != null && flag == true){
//这里的代码省略了
}else{
this.setScript("alert('must be publish template at first !');");
}
}
(2)页面中:这outputText里面的id已经去掉啦,不去掉会报错。
<script>
window.onload = new Function(" <h:outputText value="#{publishTemplate.script}"/> ");
</script>
<h:commandButton value="Publish" action="#{publishTemplate.publishOneDirectory()}" rendered="#{templateBean.select}" />
### 在 JSF 中使用 `<p:panelGrid>` 实现两列布局并正确显示内容 为了在 JSF 中实现两列布局,并正确显示报表说明、栏位说明和单元格颜色说明的内容,可以使用 PrimeFaces 提供的 `<p:panelGrid>` 组件。以下是详细的实现方法: #### 使用 `<p:panelGrid>` 实现两列布局 `<p:panelGrid>` 是一个表格组件,可以通过设置 `columns` 属性来定义列数。以下是一个示例代码,展示如何将报表说明、栏位说明和单元格颜色说明的内容以两列形式显示: ```xml <p:panelGrid columns="2" styleClass="custom-panel-grid"> <f:facet name="header"> <h:outputText value="报表说明与说明详情" /> </f:facet> <h:outputText value="报表说明:" /> <h:outputText value="这是报表的整体说明内容。" /> <h:outputText value="栏位说明:" /> <h:outputText value="这是每个栏位的具体说明。" /> <h:outputText value="单元格颜色说明:" /> <h:outputText value="不同颜色代表不同的数据状态。" /> <f:facet name="footer"> <h:outputText value="以上是所有说明内容。" /> </f:facet> </p:panelGrid> ``` #### 样式定制 为了使表格更具可读性,可以通过 CSS 自定义样式。例如,为表格添加边框、调整字体大小等。以下是一个简单的 CSS 示例: ```css .custom-panel-grid { border-collapse: collapse; width: 100%; } .custom-panel-grid td, .custom-panel-grid th { border: 1px solid #ddd; padding: 8px; text-align: left; } .custom-panel-grid tr:nth-child(even) { background-color: #f2f2f2; } ``` 将上述 CSS 添加到项目的样式文件中,或者直接嵌入到页面的 `<style>` 标签中。 #### 注意事项 - 确保 `<p:panelGrid>` 的 `columns` 属性值与实际内容匹配。如果内容数量不匹配,可能会导致布局错乱[^4]。 - 如果需要动态生成内容,可以结合 EL 表达式或循环组件(如 `<ui:repeat>`)使用[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值