简体   繁体   中英

Using <f:metadata><f:viewParam… correctly within templates

On my website I want to be able to change Locale via Link but I cannot use it as I would like. The issue is that I can specify a < f:metadata tag on each page with the following and that works

<ui:define name="metadata">
 <f:metadata>
        <f:viewParam name="locale" value="#{changeLocaleController.locale}"/>...

However that's pretty ugly since this feature has nothing to do with each single page and referes to a control element in the page header and I bet I would miss the element on same page somewhen. So I would like to shift this to my main page template or the header template instead of single pages that define the body element of my page template. My template structure is as below with one main template, one header template, a left open content section (each real page needs to fill this) and a footer template. But if I move this (obviously then <ui:define name="metadata"> ) then nothing happens if I click on the link for a reason I don't understand.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ...>
<ui:insert name="metadata" /><
<h:head>
    some css etc
</h:head>

<h:body>
    <ui:insert name="header">
        <ui:include src="header.xhtml" />
    </ui:insert>

    <ui:insert name="content" />

    <ui:insert name="footer">
        <ui:include src="footer.xhtml" />
    </ui:insert>
</h:body>

So essentially I want to move the above tag piece away from the content section to the overall template or the header section but that doesn't work. Is that possible somehow?

In my header section I have the links to the different languages with

<h:link>
  <f:param name="locale" value="#{language.locale}" />some image
</h:link>

Cheers Tom

It isn't possible to put <f:metadata> in a template. It really has to go in the client page. See also ao JSF 2 facelets <f:metadata/> in template and page .

Your best bet would be to manually grab the request parameter in the bean one of below ways:

  1. Via @ManagedProperty("#{param.xxx}") .

     @ManagedBean @RequestScoped public class ChangeLocaleController { @ManagedProperty("#{param.locale}") private String locale; // ... } 
  2. Or via ExternalContext#getRequestParameterMap() .

     @ManagedBean @RequestScoped public class ChangeLocaleController { @PostConstruct public void init() { String locale = FacesContext.getCurrentInstance() .getExternalContext().getRequestParameterMap().get("locale"); // ... } } 
  3. Or, if you're using CDI, via OmniFaces @Param .

     @Named @RequestScoped public class ChangeLocaleController { @Inject @Param private String locale; // ... } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM