如何刷新h:head中的元标记?

时间:2014-01-09 18:22:40

标签: jsf-2 facelets

在jsf2中,当我有PROJECT_STAGE =生产我的h:头部分没有刷新。当我将其更改为开发时,一切正常。如何强迫头部被刷新? h:head在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>

...或者如何以其他方式制作动态元描述?

谢谢

2 个答案:

答案 0 :(得分: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.");
}
}

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

答案 1 :(得分:0)

我在这篇文章中找到了答案:title in h:head rendered before render response phase?

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