Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 如何刷新h:head中的meta标记?_Jsf 2_Facelets - Fatal编程技术网

Jsf 2 如何刷新h:head中的meta标记?

Jsf 2 如何刷新h:head中的meta标记?,jsf-2,facelets,Jsf 2,Facelets,在jsf2中,当我有PROJECT_STAGE=Production时,我的h:head部分不会被刷新。当我把它改为开发时,一切都很好。如何强制头部进行刷新? h:head在template.xhtml中 template.xhtml: <h:head> <meta name="description" content="#{metaBean.metaDescription}" /> <meta name="keywords" content="#{

在jsf2中,当我有PROJECT_STAGE=Production时,我的h:head部分不会被刷新。当我把它改为开发时,一切都很好。如何强制头部进行刷新? h:head在template.xhtml中

template.xhtml:

<h:head>
    <meta name="description" content="#{metaBean.metaDescription}" />
    <meta name="keywords" content="#{metaBean.metaKeywords}" />
</h:head>

web.xml

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <!-- <param-value>Development</param-value>--> 
    <param-value>Production</param-value> 
</context-param>
<context-param>
    <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
    <param-value>0</param-value>
</context-param>

javax.faces.PROJECT_阶段
生产
javax.faces.FACELETS\u刷新\u周期
0
…或者如何以其他方式进行动态元描述


谢谢

您可以像这样使用元过滤器

public class HtmlMetaFilter implements Filter {

private List<String> metaNames = null;
private List<String> metaValues = null;
private int metaSize = 0;

@Override
public void destroy() {
    // logger.info("html meta filter destroyed.");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    HttpServletResponse res = (HttpServletResponse)response;
    for(int i = 0; i < metaSize; i++) {
        res.setHeader(metaNames.get(i), metaValues.get(i));
    }
    chain.doFilter(request, res);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    metaNames = Configuration.getInstance().getList("//configuration/web-app/html-meta/name");
    metaValues = Configuration.getInstance().getList("//configuration/web-app/html-meta/value");
    if(metaNames != null && metaValues != null) {
        metaSize = Math.min(metaNames.size(), metaValues.size());
    }
    // logger.info("html meta filter initialized.");
}
}
公共类HtmlMetaFilter实现过滤器{
私有列表元名称=null;
私有列表元值=null;
私有int元大小=0;
@凌驾
公共空间销毁(){
//info(“html元过滤器已销毁”);
}
@凌驾
public void doFilter(ServletRequest请求、ServletResponse响应、,
FilterChain链)抛出IOException、ServletException{
HttpServletResponse res=(HttpServletResponse)响应;
对于(int i=0;i

如果您想动态更改它,那么您需要在init方法中设置与bean文件不同的数据。

我在这篇文章中找到了答案:


使用event preRenderView并在其中准备必要的值就足够了。

我用过滤器尝试了这个想法,但它不起作用。我为测试放了一些非常简单的东西:res.setHeader(“description”,“desc的测试值”);而且它不存在于输出html中。过滤器被触发。它适用于
@RequestScoped
bean,但不适用于作用域较长的bean(例如会话作用域)。事实上,我有@SessionScoped,谢谢您的帮助