为什么我的 Struts1 I18N 找不到属性文件?

Why my Struts 1 I18N could not locate the properties files?

我正在实现一个基本的 Struts 1 i18n web 应用程序,预期输出 (multi-language.jsp) 应该类似于下面的屏幕截图:

p.s。我的项目只有英语和德语版本,属性文件分别命名为 Common.propertiesCommon_de.properties

但是,我的应用程序无法在项目中找到属性文件(位于 src/properties/$Common_CountryCode.properties$ 下)。当我点击应用程序 URL 时,控制台弹出一些警告:

Nov 15, 2018 4:45:20 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource properties/Common_en_US.properties Not Found.
Nov 15, 2018 4:45:20 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource properties/Common_en.properties Not Found.

但事实是我的项目中甚至没有这两个属性文件。再一次,当我单击首选语言时,控制台会弹出一些信息(不确定是否重要)并且浏览器将显示 404--Not Found:

Nov 15, 2018 4:50:00 PM org.apache.struts.chain.ComposableRequestProcessor init
INFO: Initializing composable request processor for module prefix ''
Nov 15, 2018 4:50:01 PM org.apache.struts.chain.commands.servlet.CreateAction createAction
INFO: Initialize action of type: action.Action

这是我的struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>

    <form-beans>
        <form-bean
            name="userForm"
            type="form.Form"/>

    </form-beans>

    <action-mappings>

        <action
            path="/LoginPage"
            type="org.apache.struts.actions.ForwardAction"
            parameter="/WebRoot/multi-language.jsp"/>

        <action
            path="/Submit"
            type="action.Action"
            name="userForm"
            validate="true"
            input="/WebRoot/multi-language.jsp"
            >
            <forward name="success" path="/WebRoot/multi-language.jsp"/>
        </action>

        <action
            path="/Locale"
            type="action.Action"
            name="userForm"
            parameter="method"
            validate="false"
            >
            <forward name="success" path="/WebRoot/multi-language.jsp"/>
        </action>

    </action-mappings>

    <message-resources
        parameter="properties.Common" />

</struts-config>

multi-language.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>multi-language.jsp</title>
</head>
<body>
<h1><bean:message key="label.common.message" /></h1>

<html:messages id="err_name" property="common.username.err">
<div style="color:red">
    <bean:write name="err_name" />
</div>
</html:messages>

<html:messages id="err_password" property="common.password.err">
<div style="color:red">
    <bean:write name="err_password" />
</div>
</html:messages>
<br />
<br />

<html:link page="/Locale.do?method=english">English</html:link>
<html:link page="/Locale.do?method=german">German</html:link>


<html:form action="/Submit">
<br />
<bean:message key="label.common.username" /> : <html:text property="username" />
<br />
<br />
<bean:message key="label.common.password" /> : <html:text property="password" />
<br />
<br />

<html:submit><bean:message key="label.common.button.submit" /></html:submit>
</html:form>


</body>
</html>

Action.java(控制器)

package action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class Action extends DispatchAction {

    public ActionForward german(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
        throws Exception {

            request.getSession().setAttribute(
                    Globals.LOCALE_KEY, Locale.GERMAN);

            return mapping.findForward("success");
        }

        public ActionForward english(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
        throws Exception {

            request.getSession().setAttribute(
                    Globals.LOCALE_KEY, Locale.ENGLISH);

            return mapping.findForward("success");
        }
}

Form.java(查看)

package form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class Form extends ActionForm{
    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if( getUsername() == null || ("".equals(getUsername())))
        {
           errors.add("common.username.err",
            new ActionMessage("error.common.username.required"));
        }

        if( getPassword() == null || ("".equals(getPassword())))
        {
           errors.add("common.password.err",
            new ActionMessage("error.common.password.required"));
        }

        return errors;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // reset properties
        username = "";
        password = "";
    }
}

如果这里的代码过多,我们深表歉意,只是想让场景更清晰。

你的 struts-config.xml 中的配置应该是这样的

  <message-resources parameter="Common" null="true" />

然后 Struts 1 (Java) 尝试使用 Common_en_USCommon_enCommon 为您的语言环境解析 ResourceBundle Inheritance。最后,如果没有找到 bundle,则抛出 MissingResourceException

负责加载资源的class,org.apache.struts.util.PropertyMessageResources,替换“.”在

    <message-resources parameter="properties.Common" />

with "/" 导致警告 Resource properties/Common_en.properties Not Found as ".properties" is being automatically added and "properties/" is added到路径。

假设您的文件位于 class 路径的某个位置,例如 WEB-INF/classes,一旦您更正配置,Struts 应该能够在没有警告的情况下找到它们。