为什么我的Struts 1 I18N无法找到属性文件?

时间:2018-11-15 09:04:23

标签: java internationalization struts struts-1

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

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

enter image description here

但是,我的应用程序无法在项目(位于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 = "";
    }
}

如果在这里的代码太多了,请道歉,只是想让情况变得清晰起来。

1 个答案:

答案 0 :(得分:1)

The configuration in your struts-config.xml should be something like this

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

Then Struts 1 (Java) tries to resolve the ResourceBundle Inheritance for your locale using Common_en_US, Common_en and Common. In the end, if no bundle is found, MissingResourceException is thrown.

The class responsible for loading of resources, org.apache.struts.util.PropertyMessageResources, replaces "." in

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

with "/" resulting in your warning Resource properties/Common_en.properties Not Found as ".properties" is being added automatically and "properties/" is added to the path.

Assuming your files lie somewhere on the classpath, like WEB-INF/classes, Struts should be able to find them without warning once you correct the config.