我可以更新标题栏吗?(can I update the title bar?)

编程入门 行业动态 更新时间:2024-10-28 08:22:45
我可以更新标题栏吗?(can I update the title bar?)

在上一个问题中,我询问了有关更新菜单栏的问题。 BalusC告诉我,我需要添加包含菜单栏的表单。

我想扩展这个问题,询问我是否可以更新标题中的文字。 正在使用模板,我使用填写值

<ui:define name="AreaTitle"> #{viewBacking.current.firstName} #{viewBacking.current.surName} </ui:define>

模板有

<h:head> <title><ui:insert name="AreaTitle">Master template</ui:insert></title> <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> </h:head>

在标题中定义表单似乎很奇怪,因此没有定义。 我在viewBacking.current中设置了一个断点,所以我可以看到它何时使用它。 即使我点击刷新以重新显示表单,它也不会再次达到断点。 只有当我转到具有不同内容的不同页面时,它才会再次达到断点。 刷新是

public void refreshForm() { RequestContext context = RequestContext.getCurrentInstance(); context.update("menuForm:masterMenuBar"); context.update("AreaTitle"); }

这显示了BalusC在masterMenuBar上给我的先前解决方案。 很可能我不能做我要求做的事,但我想确认是否是这种情况。

谢谢,伊兰

In a previous question I asked about updating a menu bar. BalusC told me that I need to add the form containing the menu bar.

I would like to extend that question to ask if I can update the text in the title. A template is being used and I fill in the value using

<ui:define name="AreaTitle"> #{viewBacking.current.firstName} #{viewBacking.current.surName} </ui:define>

The template has

<h:head> <title><ui:insert name="AreaTitle">Master template</ui:insert></title> <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> </h:head>

It seems strange to define a form in the header so none is defined. I put a break point in the viewBacking.current so I can see when it uses it. Even if I hit the refresh to redisplay the form, it won't hit the break point again. Only when I go to a different page with different content does it hit the break point again. The for the refresh is

public void refreshForm() { RequestContext context = RequestContext.getCurrentInstance(); context.update("menuForm:masterMenuBar"); context.update("AreaTitle"); }

This shows the previous solution which BalusC gave me on the masterMenuBar. It could well be that I can't do what I'm asking to do, but I'd like confirmation if that is the case.

Thanks, Ilan

最满意答案

您无法通过JSF ajax更新更新标题,因为<title>不是JSF组件。 你也不能把HTML或JSF组件放在<title> ,这是非法的 HTML语法。

最好的办法是使用JavaScript来更新标题,方法是将其分配给document.title 。 您可以使用RequestContext#execute() 。

String fullName = current.getFirstName() + " " + current.getSurName(); context.execute("document.title='" + fullName + "'");

由于这似乎是用户控制的数据,我将使用StringEscapeUtils#escapeJavaScript()来逃避它,以防止潜在的XSS攻击漏洞 。

String fullName = current.getFirstName() + " " + current.getSurName(); context.execute("document.title='" + StringEscapeUtils.escapeJavaScript(fullName) + "'");

另一种方法是使用OmniFaces <o:onloadScript> 。

<o:onloadScript>document.title='#{of:escapeJS(viewBacking.current.firstName)} #{of:escapeJS(viewBacking.current.surName)}'</o:onloadScript>

这将在每个ajax请求上重新执行。

You can't update the title by JSF ajax update simply because <title> is not a JSF component. You also can't put HTML or JSF components in <title>, this is illegal HTML syntax.

Your best bet is to use JavaScript instead to update the title by assigning it to the document.title. You can use RequestContext#execute() for this.

String fullName = current.getFirstName() + " " + current.getSurName(); context.execute("document.title='" + fullName + "'");

Since this seems to be user-controlled data, I would use StringEscapeUtils#escapeJavaScript() to escape it in order to prevent potential XSS attack holes.

String fullName = current.getFirstName() + " " + current.getSurName(); context.execute("document.title='" + StringEscapeUtils.escapeJavaScript(fullName) + "'");

An alternative is to use OmniFaces <o:onloadScript>.

<o:onloadScript>document.title='#{of:escapeJS(viewBacking.current.firstName)} #{of:escapeJS(viewBacking.current.surName)}'</o:onloadScript>

This will be re-executed on every ajax request.

更多推荐

本文发布于:2023-08-07 20:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标题栏   update   bar   title

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!