Struts国際化またはローカライゼーションの例

Strutsの国際化またはローカライズの例

これは、指定された選択言語に基づいてメッセージまたはコンテンツベースを表示するStrutsローカライズの例です。 デフォルトでは、Strutsは各ユーザーのロケール属性をセッションコンテキストのキー「org.apache.struts.action.LOCALE」の下に保存します。必要なのは、このセッション属性をいじるだけです。

このStrutsローカリゼーションの例をダウンロード–Struts-Localization-Example.zip

1. プロジェクト構造

これがプロジェクト構造です。

Struts-localization-folder

2. プロパティファイル

すべてのローカライズメッセージは、「filename_locale_code.properties」の形式のプロパティファイルで宣言されます。 ロケールコードはJava.Util.Localeクラスで確認できます。 e.g

  • 英語– Common.propertiesまたはCommon_en_US.properties

  • 中国語– Common_zh_CN.properties

  • フランス– Common_fr.properties

  • ドイツ語– Common_de.properties

Common.properties

#error message
error.common.username.required = Username is required
error.common.password.required = Password is required

#label message
label.common.message = localization example
label.common.username = Username
label.common.password = Password
label.common.button.submit = Submit

Common_de.properties

#error message
error.common.username.required = Benutzername ist erforderlich
error.common.password.required = Passwort ist erforderlich

#label message
label.common.message = Lokalisierung Beispiel
label.common.username = Benutzername
label.common.password = Kennwort
label.common.button.submit = Einreichen

3. アクションクラス

複数のアクションのためにDispatchActionを拡張するアクションクラスを作成します。

package com.example.common.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 LanguageSelectAction extends DispatchAction{

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

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

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

    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 france(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
    throws Exception {

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

        return mapping.findForward("success");
    }

}

4. アクションフォーム

ユーザー名とパスワードのデータを保持するアクションフォームを作成し、フォームの検証も行います。

package com.example.common.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 UserForm 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 = "";
    }
}

5. JSP

言語の選択、フォームの値、およびエラーメッセージを表示するJSPページを作成します。 「/Locale.do?method=chinese」は、DispatchActionchinese()メソッドと一致します。


 +
: +
 +
: +
 +

6. struts-config.xml

すべてまとめてください。





    
        

    

    

        

        
            
        

        
            
        

    

    

7. wel.xml

Webデプロイメント記述子ファイルにStrutsフレームワークを統合します。




  Maven Struts Examples

  
    action
    
        org.apache.struts.action.ActionServlet
    
    
        config
        
         /WEB-INF/struts-config.xml
        
    
    1
  

  
       action
       *.do
  

スクリーンショット

言語リンクによってインターフェイスを変更できます。

英語インターフェース

Struts-localization-english

中国語インターフェース

Struts-localization-chinese

フランスのインターフェース

Struts-localization-france

ドイツ語インターフェース

Struts-localization-german