0

I am using facelets in my views with JSF 2.0. I have two backing beans, two xhtml view files and another xhtml template file. When I have the second view in the webapp directory and I change the language all is Ok but when I put mi second view into the WEB-INF folder and I change the language I have the following error in the chrome network console:

POST http://localhost:8080/Languages/WEB-INF/inventory.xhtml 404 (No Encontrado)

These are my backings beans:

LanguageBean.java:

package com.testapp;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "language")
@SessionScoped
public class LanguageBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String localeCode;
    private static final String DEFAULT_LOCAL_CODE = "es";

    private static Map<String, Object> countries;
    static {
        countries = new LinkedHashMap<String, Object>();
        countries.put("English", Locale.ENGLISH); // label, value
        countries.put("Français", Locale.FRENCH);
        countries.put("Español", new Locale("es"));
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }

    public String getLocaleCode() {
        if(localeCode == null) {
            localeCode = DEFAULT_LOCAL_CODE;
        }
        return localeCode;
    }

    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }

    public void changeLanguage() {

        // loop a map to compare the locale code
        for (Map.Entry<String, Object> entry : countries.entrySet()) {

            if (entry.getValue().toString().equals(localeCode)) {

                FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());

            }
        }

    }

}

InventoryBean.java:

package com.testapp;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ManagedBean(name = "inventory")
@ViewScoped
public class InventoryBean implements Serializable {

    private static final long serialVersionUID = 4576404491584185639L;

    Logger logger = LoggerFactory.getLogger(InventoryBean.class);

    // Failing path
private static final String INVENTORY_MAIN_VIEW = "/WEB-INF/inventory";
// Working path
    // private static final String INVENTORY_MAIN_VIEW = "/views/inventory";

    public String findProducts() {

        try {
            System.err.println("In inventory backing bean");
        } catch (Exception exception) {
        }

        return INVENTORY_MAIN_VIEW;

    }

    public void changeLanguage() {
        FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale.ENGLISH));
    }

}

These are the view files:

default.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/WEB-INF/header.xhtml">
    <ui:define name="contentBody">
        <f:view locale="#{language.localeCode}">
            <h:form id="contentForm">
                <h:outputText value="#{msg['internationalization.test']}" />
                <p:commandButton id="gettingProducts"
                    value="Getting the products..." action="#{inventory.findProducts}" />
            </h:form>
        </f:view>
    </ui:define>
</ui:composition>

inventory.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/WEB-INF/header.xhtml">
    <ui:define name="contentBody">
        <f:view locale="#{language.localeCode}">
            <h:form id="contentForm">
                <h:outputText value="#{msg['internationalization.test']}" />
            </h:form>
        </f:view>
    </ui:define>
</ui:composition>

And this is the template xhtml file (header.xhtml):

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
    <h:head>
    </h:head>
    <h:body>
        <div id="content">
            <h:form id="headerForm">
                <h:panelGrid columns="2">
                    <p:outputLabel value="Language :" for="languages" />
                    <h:selectOneMenu required="true" id="languages"
                        value="#{language.localeCode}">
                        <f:selectItems value="#{language.countriesInMap}" />
                        <p:ajax event="change" update="@all"
                            listener="#{language.changeLanguage}" />
                    </h:selectOneMenu>
                </h:panelGrid>
            </h:form>
            <ui:insert name="contentBody" />
        </div>
    </h:body>
</f:view>
</html>

What is happening?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72

1 Answers1

1

A HTTP 404 error simply means "Page Not Found". And indeed, files inside /WEB-INF folder are not publicly accessible. Put files which are intented to be publicly accessible outside /WEB-INF folder.

Note that this has further completely nothing to do with internationalization. You'd have had exactly the same problem when not doing anything with regard to internationalization (e.g. a simple navigation).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555