1

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

TomFree
  • 1,091
  • 3
  • 17
  • 31
  • btw if I replace `` in my main template and put in ` ` this does not work either. Pretty weird to me since basically it's the same code with the one that works except the location of the tags is in a different file... – TomFree Aug 06 '16 at 15:06
  • tunrs out on http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/metadata.html that I cannot put in the template. "This tag must reside within the top level XHTML file for the given viewId, or in a template client, but not in a template." Any ideas how else I can build this without having to put it on every single page? I.e. why doesn't it work if I put it into the header? – TomFree Aug 06 '16 at 15:30
  • It doesn't work if you put it intot the template because of what it says in the text you quoted. – user207421 Aug 08 '16 at 01:23

1 Answers1

2

It isn't possible to put <f:metadata> in a template. It really has to go in the client page. See also a.o. 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;
    
        // ...
    }
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555