0

JSF 2.0 のビューで facelets を使用しています。2 つのバッキング Bean、2 つの xhtml ビュー ファイル、および別の xhtml テンプレート ファイルがあります。webapp ディレクトリに 2 番目のビューがあり、言語を変更するとすべて問題ありませんが、2 番目のビューを 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));
    }

}

ビュー ファイルは次のとおりです。

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>

在庫.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 に答える