struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-struts/web/example index.jsp logon.jsp
Date Wed, 27 Dec 2000 02:29:26 GMT
craigmcc    00/12/26 18:29:26

  Modified:    src/doc  struts-form.xml
               src/share/org/apache/struts/action ActionServlet.java
               src/share/org/apache/struts/taglib/form HtmlTag.java
               web/example index.jsp logon.jsp
  Log:
  Add a new "locale" attribute to the <form:html> tag so that you can
  request Struts to store a Locale in the user's session if one does not
  already exist, based on the request headers, equivalent to the
  processLocale() method of the default servlet.
  
  Remove redundant logic from ActionServlet.processLocale() -- we now count
  on the servlet container to do this correctly for us.  Any compliant 2.2
  container must implement ServletRequest.processLocale().
  
  Update the example application to use the new attribute, like this:
  
  	<form:html locale="true">
  	...
  	</form:html>
  
  Submitted by:	Kare Nuorteva <kare.nuorteva@satama.com>
  
  Revision  Changes    Path
  1.19      +16 -0     jakarta-struts/src/doc/struts-form.xml
  
  Index: struts-form.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/doc/struts-form.xml,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- struts-form.xml	2000/12/26 17:42:38	1.18
  +++ struts-form.xml	2000/12/27 02:29:24	1.19
  @@ -1166,9 +1166,25 @@
             </info>
   
             <attribute>
  +            <name>locale</name>
  +            <required>false</required>
  +            <rtexprvalue>true</rtexprvalue>
  +            <info>
  +              <p>Set to <code>true</code> in order to record a Locale
based on
  +              the current request's <code>Accept-Language</code> header (if
  +              any) if none has currently been set.</p>
  +            </info>
  +          </attribute>
  +
  +          <attribute>
               <name>xhtml</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
  +            <info>
  +              <p>Set to <code>true</code> in order to render an
  +              <code>xml:lang</code> attribute on the generated
  +              <code>html</code> element.</p>
  +            </info>
             </attribute>
   
           </tag>
  
  
  
  1.44      +5 -43     jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java
  
  Index: ActionServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- ActionServlet.java	2000/12/27 00:16:03	1.43
  +++ ActionServlet.java	2000/12/27 02:29:25	1.44
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v
1.43 2000/12/27 00:16:03 craigmcc Exp $
  - * $Revision: 1.43 $
  - * $Date: 2000/12/27 00:16:03 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v
1.44 2000/12/27 02:29:25 craigmcc Exp $
  + * $Revision: 1.44 $
  + * $Date: 2000/12/27 02:29:25 $
    *
    * ====================================================================
    *
  @@ -212,7 +212,7 @@
    * </ul>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.43 $ $Date: 2000/12/27 00:16:03 $
  + * @version $Revision: 1.44 $ $Date: 2000/12/27 02:29:25 $
    */
   
   public class ActionServlet
  @@ -1567,50 +1567,12 @@
               return;             // Locale object is already present
   
           // Use the Locale returned by the servlet container (if any)
  -        Locale locale = null;
  -        try {
  -            locale = request.getLocale();
  -        } catch (Throwable t) {
  -            locale = null;      // Method not present in servlet 2.1
  -        }
  +        Locale locale = request.getLocale();
           if (locale != null) {
               if (debug >= 1)
                   log("Setting locale '" + locale + "'");
               session.setAttribute(Action.LOCALE_KEY, locale);
  -            return;
  -        }
  -
  -        // Calculate a Locale based on the HTTP headers with this request
  -        String value = request.getHeader("Accept-Language");
  -        if (value == null) {
  -            session.setAttribute(Action.LOCALE_KEY, defaultLocale);
  -            return;             // No Accept-Language header was present
           }
  -
  -        int comma = value.indexOf(',');
  -        if (comma >= 0)
  -            value = value.substring(0, comma);  // Use first entry only
  -        int semi = value.indexOf(';');
  -        if (semi >= 0)
  -            value = value.substring(0, semi);   // Strip quality ranking
  -
  -        String language = null;
  -        String country = null;
  -        int dash = value.indexOf('-');
  -        if (dash < 0) {
  -            language = value.trim();
  -            country = "";
  -        } else {
  -            language = value.substring(0, dash).trim();
  -            country = value.substring(dash + 1).trim();
  -        }
  -        locale = new Locale(language, country);
  -
  -        // Store the calculated Locale in the user's session
  -        if (debug >= 1)
  -            log("Setting locale '" + locale + "' for header '" +
  -                request.getHeader("Accept-Language") + "'");
  -        session.setAttribute(Action.LOCALE_KEY, locale);
   
       }
   
  
  
  
  1.4       +35 -7     jakarta-struts/src/share/org/apache/struts/taglib/form/HtmlTag.java
  
  Index: HtmlTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/form/HtmlTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HtmlTag.java	2000/11/22 20:08:22	1.3
  +++ HtmlTag.java	2000/12/27 02:29:25	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/form/HtmlTag.java,v
1.3 2000/11/22 20:08:22 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/11/22 20:08:22 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/form/HtmlTag.java,v
1.4 2000/12/27 02:29:25 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/12/27 02:29:25 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * there is a current Locale available in the user's session.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.3 $ $Date: 2000/11/22 20:08:22 $
  + * @version $Revision: 1.4 $ $Date: 2000/12/27 02:29:25 $
    */
   
   public class HtmlTag extends TagSupport {
  @@ -90,6 +90,20 @@
   
   
       /**
  +     * Should we set the current Locale for this user if needed?
  +     */
  +    protected boolean locale = false;
  +
  +    public boolean getLocale() {
  +        return (locale);
  +    }
  +
  +    public void setLocale(boolean locale) {
  +        this.locale = locale;
  +    }
  +
  +
  +    /**
        * Are we rendering an xhtml page?
        */
       protected boolean xhtml = false;
  @@ -112,9 +126,20 @@
   
           StringBuffer sb = new StringBuffer("<html");
           HttpSession session = pageContext.getSession();
  -        Locale locale = (Locale) session.getAttribute(Action.LOCALE_KEY);
  -        if (locale != null) {
  -            String lang = locale.getLanguage();
  +
  +        // Set the current Locale if necessary
  +        if (locale &&
  +            (session.getAttribute(Action.LOCALE_KEY) == null)) {
  +            Locale newLocale = pageContext.getRequest().getLocale();
  +            if (newLocale != null)
  +                session.setAttribute(Action.LOCALE_KEY, newLocale);
  +        }
  +
  +        // Use the current Locale to set our language preferences
  +        Locale currentLocale =
  +            (Locale) session.getAttribute(Action.LOCALE_KEY);
  +        if (currentLocale != null) {
  +            String lang = currentLocale.getLanguage();
               if ((lang != null) && (lang.length() > 0)) {
                   sb.append(" lang=\"");
                   sb.append(lang);
  @@ -127,6 +152,8 @@
               }
           }
           sb.append(">");
  +
  +        // Write out the beginning tag for this page
           JspWriter out = pageContext.getOut();
           try {
               out.println(sb.toString());
  @@ -167,6 +194,7 @@
        */
       public void release() {
   
  +        locale = false;
           xhtml = false;
   
       }
  
  
  
  1.11      +1 -1      jakarta-struts/web/example/index.jsp
  
  Index: index.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/index.jsp,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- index.jsp	2000/11/22 20:08:22	1.10
  +++ index.jsp	2000/12/27 02:29:25	1.11
  @@ -3,7 +3,7 @@
   <%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form" %>
   <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
   
  -<form:html>
  +<form:html locale="true">
   <head>
   <title><bean:message key="index.title"/></title>
   <form:base/>
  
  
  
  1.12      +1 -1      jakarta-struts/web/example/logon.jsp
  
  Index: logon.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/logon.jsp,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- logon.jsp	2000/12/08 04:34:04	1.11
  +++ logon.jsp	2000/12/27 02:29:25	1.12
  @@ -3,7 +3,7 @@
   <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
   <%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form" %>
   
  -<form:html>
  +<form:html locale="true">
   <head>
   <title><bean:message key="logon.title"/></title>
   <form:base/>
  
  
  

Mime
View raw message