struts源码分析(3)-----国际化!

我们在进行国际化的时候,主要要依赖到下面的两个类,这是我进行的一个国际化的实例;

package org.corey.struts;

import java.text.MessageFormat;
import java.util.ResourceBundle;

public class I18NDDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ResourceBundle resource = ResourceBundle.getBundle("a");
        MessageFormat formator=new MessageFormat(resource.getString("aa"));
        String[] arg=new String[2];
        arg[0]="corey";
        arg[1]="helloworld";
        String result=formator.format(arg);
        System.out.println(result);
    }

}




我们来观察一下struts下的国际化的实现;


我们进行国际化的时候,代码如下:

Locale locale=new Locale("zh","CN");

在action中,我们可以直接调用:

this.setLocale(request,locale);

我们看一下这个方法的代码:

    protected void setLocale(HttpServletRequest request, Locale locale) {
        HttpSession session = request.getSession();

        if (locale == null) {
            locale = Locale.getDefault();
        }

        session.setAttribute(Globals.LOCALE_KEY, locale);
    }

可见,不要被参数的假象所迷惑一位这个本地化对象是放在request中的,而是在session中,并且key是Globals.LOCALE_KEY

public static final String LOCALE_KEY = "org.apache.struts.action.LOCALE";

放在session中的这个对象如何使用呢?

在struts中,我们用来进行国际化的主要的工具就是

<bean:message>

我们来看一下这个标签所做的事情:

在其标签实现类中有如下方法:

  public int doStartTag() throws JspException {
        // Initialize for a new request.
        processed = false;

        // Were any messages specified?
        ActionMessages messages = null;

        // Make a local copy of the name attribute that we can modify.
        String name = this.name;

        if ((message != null) && "true".equalsIgnoreCase(message)) {
            name = Globals.MESSAGE_KEY;
        }

        try {
            messages =
                TagUtils.getInstance().getActionMessages(pageContext, name);
        } catch (JspException e) {
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }

        // Acquire the collection we are going to iterate over
        this.iterator =
            (property == null) ? messages.get() : messages.get(property);

        // Store the first value and evaluate, or skip the body if none
        if (!this.iterator.hasNext()) {
            return SKIP_BODY;
        }

        // process the first message
        processMessage((ActionMessage) iterator.next());

        if ((header != null) && (header.length() > 0)) {
            String headerMessage =
                TagUtils.getInstance().message(pageContext, bundle, locale,
                    header);

            if (headerMessage != null) {
                TagUtils.getInstance().write(pageContext, headerMessage);
            }
        }

方法:

 TagUtils.getInstance().message(pageContext, bundle, locale,
                    header);

如下:

public String message(PageContext pageContext, String bundle,
        String locale, String key, Object[] args)
        throws JspException {
        MessageResources resources =
            retrieveMessageResources(pageContext, bundle, false);

        Locale userLocale = getUserLocale(pageContext, locale);
        String message = null;

        if (args == null) {
            message = resources.getMessage(userLocale, key);
        } else {
            message = resources.getMessage(userLocale, key, args);
        }

        if ((message == null) && log.isDebugEnabled()) {
            // log missing key to ease debugging
            log.debug(resources.getMessage("message.resources", key, bundle,
                    locale));
        }

        return message;
    }

getUserLocale取得本地对象:

  public Locale getUserLocale(PageContext pageContext, String locale) {
        return RequestUtils.getUserLocale((HttpServletRequest) pageContext
            .getRequest(), locale);
    }


你可能感兴趣的:(String,session,struts,header,null,debugging)