0

我在 JSF 2.0 的视图中使用 facelets。我有两个支持 bean、两个 xhtml 视图文件和另一个 xhtml 模板文件。当我在 webapp 目录中有第二个视图并更改语言时一切正常,但是当我将第二个视图放入 WEB-INF 文件夹并更改语言时,我在 chrome 网络控制台中出现以下错误:

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

这些是我的支持豆:

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));
    }

}

这些是视图文件:

默认.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>

库存.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>

这是模板 xhtml 文件(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>

怎么了?

4

1 回答 1

1

HTTP 404 错误仅表示“找不到页面”。事实上,文件/WEB-INF夹内的文件是不可公开访问的。将打算公开访问的文件放在文件/WEB-INF夹之外。

请注意,这与国际化完全无关。当不做任何关于国际化的事情(例如简单的导航)时,您会遇到完全相同的问题。

也可以看看:

于 2013-08-23T16:54:11.097 回答