husted      2004/03/09 19:14:31

  Modified:    contrib/struts-faces/src/example/org/apache/struts/webapp/example
                        Constants.java
               web/example tour.html welcome.jsp
               web/example/WEB-INF struts-config.xml
  Added:       src/example/org/apache/struts/webapp/example
                        LocaleAction.java
  Log:
  Add change locale feature.
  
  Revision  Changes    Path
  1.3       +1 -2      
jakarta-struts/contrib/struts-faces/src/example/org/apache/struts/webapp/example/Constants.java
  
  Index: Constants.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/contrib/struts-faces/src/example/org/apache/struts/webapp/example/Constants.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Constants.java    8 Mar 2004 02:49:52 -0000       1.2
  +++ Constants.java    10 Mar 2004 03:14:31 -0000      1.3
  @@ -54,5 +54,4 @@
        */
       public static final String USER_KEY = "user";
   
  -
   }
  
  
  
  1.1                  
jakarta-struts/src/example/org/apache/struts/webapp/example/LocaleAction.java
  
  Index: LocaleAction.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/example/org/apache/struts/webapp/example/LocaleAction.java,v
 1.1 2004/03/10 03:14:31 husted Exp $
   * $Revision: 1.1 $
   * $Date: 2004/03/10 03:14:31 $
   *
   * Copyright 2000-2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.struts.webapp.example;
  
  import java.util.Locale;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.apache.struts.Globals;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  
  
  /**
   * Change user's Struts @link(java.util.Locale.
   */
  public final class LocaleAction extends BaseAction {
  
      /**
       * <p>Return true if parameter is null or trims to empty.</p>
       * @param string The string to text; may be  null
       * @return true if parameter is null or empty
       */
      private boolean isBlank(String string) {
          return ((string==null) || (string.trim().length()==0));
      }
  
      /**
       * Parameter for @link(java.util.Locale) language property.
       */
      private static final String LANGUAGE = "language" ;
  
      /**
       * Parameter for @link(java.util.Locale) country property.
       */
      private static final String COUNTRY = "country";
  
      /**
       * Parameter for response page URI.
       */
      private static final String PAGE = "page";
  
      /**
       * Parameter for response forward name.
       */
      private static final String FORWARD = "forward";
  
      /**
       * Logging message if LocaleAction is missing a target parameter.
       */
      private static final String LOCALE_LOG = "LocaleAction: Missing page or forward 
parameter";
  
      /**
       * <p>
       * Change the user's Struts @link(java.util.Locale) based on request
       * parameters for "language", "country".
       * After setting the Locale, control is forwarded to an URI path
       * indicated by a "page" parameter, or a forward indicated by a
       * "forward" parameter, or to a forward given as the mappings
       * "parameter" property.
       * The response location must be specified one of these ways.
       * </p>
       * @param mapping The ActionMapping used to select this instance
       * @param form The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @return An ActionForward indicate the resources that will render the response
       * @exception Exception if an input/output error or servlet exception occurs
       */
      public ActionForward execute(ActionMapping mapping,
                   ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response)
      throws Exception {
  
          String language = request.getParameter(LANGUAGE);
          String country = request.getParameter(COUNTRY);
  
          Locale locale = getLocale(request);
  
          if ((!isBlank(language)) && (!isBlank(country))) {
              locale = new Locale(language, country);
          }
          else if (!isBlank(language)) {
              locale = new Locale(language, "");
          }
  
          HttpSession session = request.getSession();
          session.setAttribute(Globals.LOCALE_KEY, locale);
  
          String target = request.getParameter(PAGE);
          if (!isBlank(target)) return new ActionForward(target);
  
          target = request.getParameter(FORWARD);
          if (isBlank(target)) target = mapping.getParameter();
          if (isBlank(target)) {
              log.warn(LOCALE_LOG);
              return null;
          }
          return mapping.findForward(target);
      }
  }
  
  
  
  1.6       +16 -7     jakarta-struts/web/example/tour.html
  
  Index: tour.html
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/tour.html,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- tour.html 9 Mar 2004 04:35:01 -0000       1.5
  +++ tour.html 10 Mar 2004 03:14:31 -0000      1.6
  @@ -135,7 +135,9 @@
   
       <p>Here, the "WelcomeAction" Java class executes whenever someone asks for the 
welcome action. As it completes, the Action class can select which page is displayed. 
Two pages the class can select here are "Error.jsp" and "welcome.jsp". But the Action 
class doesn't need to know the path to the pages. The class can select them just using 
the names "success" or "failure".</p>
   
  -    <p>The MailReader application retains a list of users along with their email 
accounts. The application stores this information in a database. If the application 
can't connect to the database, the application can't do its job. So before displaying 
the welcome page, the class checks to see if the database is available. The MailReader 
is also internationalized application. So, the WelcomeAction checks to see if the 
message resources are available too. If both resources are available, the class 
forwards to the "success" path. Otherwise, it forwards to the "failure" path so that 
the appropriate error messages can be displayed.</p>
  +    <p>OK ... but why would a WelcomeAction want to choose between success and 
failure?</p>
  +
  +    <p>The MailReader application retains a list of users along with their email 
accounts. The application stores this information in a database. If the application 
can't connect to the database, the application can't do its job. So before displaying 
the welcome page, the class checks to see if the database is available. The MailReader 
is also an internationalized application. So, the WelcomeAction checks to see if the 
message resources are available too. If both resources are available, the class 
forwards to the "success" path. Otherwise, it forwards to the "failure" path so that 
the appropriate error messages can be displayed.</p>
   
       <h4><a name="MemoryDatabasePlugIn.java" 
id="MemoryDatabasePlugIn.java">MemoryDatabasePlugIn.java</a></h4>
   
  @@ -212,19 +214,26 @@
   &lt;li>&lt;html:link action="/logon">&lt;bean:message 
key="index.logon"/>&lt;/html:link>&lt;/li>
   &lt;/ul>
   
  -&lt;p>&lt;html:link action="/tour">&lt;bean:message 
key="index.tour"/>&lt;/html:link>&lt;/p>
  +&lt;h3>Change Language&lt;/h3>
  +&lt;ul>
  +&lt;li>&lt;html:link action="/locale?language=en">English&lt;/html:link>&lt;/li>
  +&lt;li>&lt;html:link action="/locale?language=ja" 
useLocalEncoding="true">Japanese&lt;/html:link>&lt;/li>
  +&lt;li>&lt;html:link action="/locale?language=ru" 
useLocalEncoding="true">Russian&lt;/html:link>&lt;/li>
  +&lt;/ul>
  +
  +&lt;hr />
   
   &lt;p>&lt;html:img bundle="alternate" pageKey="struts.logo.path" 
altKey="struts.logo.alt"/>&lt;/p>
   
  +&lt;p>&lt;html:link action="/tour">&lt;bean:message 
key="index.tour"/>&lt;/html:link>&lt;/p>
  +
   &lt;/body>
   &lt;/html></code></pre>
   </blockquote>
   
  -    <p>At the top of the welcome.jsp page, there are several directives that load 
the Struts tag libraries. These are just the usual red tape that goes with any JSP 
file.</p>
  -
  -    <p>The rest of the page demonstrates three Struts JSP tags: "bean:message", 
"html:link", and "html:img".</p>
  +    <p>At the top of the welcome.jsp page, there are several directives that load 
the Struts tag libraries. These are just the usual red tape that goes with any JSP 
file. The rest of the page demonstrates three Struts JSP tags: "bean:message", 
"html:link", and "html:img".</p>
   
  -    <p>The bean:message tag inserts a message from the MessageResources file.</p>
  +    <p>The bean:message tag inserts a message from the MessageResources file. The 
MailReader comes with support for three locales: English (the default), Russian, and 
Japanese. If the Struts locale setting is changed for a user, the bean:message tag 
will render messages from that locale's property bundle instead. </p>
   
       <p>The html:link tag does double duty. First, you can refer to an action or 
forward stored in the Struts configuration, and the tag will insert the corresponding 
path when the page is rendered. This makes it easy to "rewire" an application without 
touching all the pages. Second, the link tag will "URL encode" the hyperlink to 
maintain the client session. Your application can maintain client state without 
requiring cookies.</p>
   
  @@ -244,7 +253,7 @@
         <li>Automatically URL-encoded paths as needed.</li>
       </ul>
   
  -    <p>When rendered, the welcome page lists two "options": one to register with 
the application and one to login in (if you have already registered). Let's follow the 
login link first.</p>
  +    <p>When rendered, the welcome page lists two menu options: one to register with 
the application and one to login in (if you have already registered). Let's follow the 
login link first.</p>
   
       <h3><a name="logon.jsp" id="logon.jsp">logon.jsp</a></h3>
   
  
  
  
  1.4       +10 -1     jakarta-struts/web/example/welcome.jsp
  
  Index: welcome.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/welcome.jsp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- welcome.jsp       9 Mar 2004 04:35:01 -0000       1.3
  +++ welcome.jsp       10 Mar 2004 03:14:31 -0000      1.4
  @@ -14,9 +14,18 @@
   <li><html:link action="/logon"><bean:message key="index.logon"/></html:link></li>
   </ul>
   
  -<p><html:link action="/tour"><bean:message key="index.tour"/></html:link></p>
  +<h3>Language Options</h3>
  +<ul>
  +<li><html:link action="/locale?language=en">English</html:link></li>
  +<li><html:link action="/locale?language=ja" 
useLocalEncoding="true">Japanese</html:link></li>
  +<li><html:link action="/locale?language=ru" 
useLocalEncoding="true">Russian</html:link></li>
  +</ul>
  +
  +<hr />
   
   <p><html:img bundle="alternate" pageKey="struts.logo.path" 
altKey="struts.logo.alt"/></p>
  +
  +<p><html:link action="/tour"><bean:message key="index.tour"/></html:link></p>
   
   </body>
   </html>
  
  
  
  1.40      +10 -3     jakarta-struts/web/example/WEB-INF/struts-config.xml
  
  Index: struts-config.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/WEB-INF/struts-config.xml,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- struts-config.xml 9 Mar 2004 04:40:53 -0000       1.39
  +++ struts-config.xml 10 Mar 2004 03:14:31 -0000      1.40
  @@ -50,6 +50,7 @@
   
     <!-- ========== Global Forward Definitions ============================== -->
     <global-forwards>
  +    <forward   name="welcome"              path="/welcome.do"/>
       <forward   name="logoff"               path="/logoff.do"/>
       <forward   name="logon"                path="/logon.do"/>
       <forward   name="success"              path="/mainMenu.do"/>
  @@ -65,6 +66,12 @@
             <forward name="failure" path="/Error.jsp" />
             <forward name="success" path="/welcome.jsp" />
         </action>
  +
  +      <!-- Change locale action -->
  +      <action
  +          path="/locale"
  +          type="org.apache.struts.webapp.example.LocaleAction"
  +          parameter="welcome" />
   
         <!-- Display registration page -->
         <action    path="/registration"
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to