View Javadoc

1   /*
2    * $Id: LoadMessagesTag.java 471754 2006-11-06 14:55:09Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.faces.taglib;
23  
24  
25  import java.util.Locale;
26  
27  import javax.faces.context.FacesContext;
28  
29  import javax.servlet.jsp.PageContext;
30  import javax.servlet.jsp.tagext.TagSupport;
31  
32  import org.apache.struts.Globals;
33  import org.apache.struts.faces.util.MessagesMap;
34  import org.apache.struts.util.MessageResources;
35  
36  
37  /**
38   * <p>Tag that exposes a specified <code>MessageResources</code> instance
39   * as <code>Map</code>, so that the embedded messages may be retrieved via
40   * value binding expressions.</p>
41   */
42  
43  public class LoadMessagesTag extends TagSupport {
44  
45  
46      // ---------------------------------------------------------- Tag Attributes
47  
48  
49      /**
50       * <p>The name of the <code>MessageResources</code> to expose, or
51       * <code>null</code> for the default <code>MessageResources</code>
52       * for this application module.</p>
53       */
54      private String messages = null;
55      public void setMessages(String messages) {
56          this.messages = messages;
57      }
58  
59  
60      /**
61       * <p>The request attribute key under which a <code>Map</code>
62       * will be exposed.</p>
63       */
64      private String var = null;
65      public void setVar(String var) {
66          this.var = var;
67      }
68  
69  
70      // ------------------------------------------------------------- Tag Methods
71  
72  
73      /**
74       * <p>Expose a <code>Map</code> wrapping the specified
75       * <code>MessageResources</code> instance, for the <code>Locale</code>
76       * specified in the view root component of the current view.</p>
77       */
78      public int doStartTag() {
79  
80          // Acquire the Locale to be wrapped
81          Locale locale =
82              FacesContext.getCurrentInstance().getViewRoot().getLocale();
83  
84          // Acquire the MessageResources to be wrapped
85          MessageResources messages = null;
86          if (this.messages == null) {
87              messages = (MessageResources)
88                  pageContext.getAttribute(Globals.MESSAGES_KEY,
89                                           PageContext.REQUEST_SCOPE);
90              if (messages == null) {
91                  messages = (MessageResources)
92                      pageContext.getAttribute(Globals.MESSAGES_KEY,
93                                               PageContext.APPLICATION_SCOPE);
94              }
95          } else {
96              messages = (MessageResources)
97                  pageContext.getAttribute(this.messages,
98                                           PageContext.APPLICATION_SCOPE);
99          }
100 
101         // Expose a Map instance under the specified request attribute key
102         pageContext.setAttribute(var,
103                                  new MessagesMap(messages, locale),
104                                  PageContext.REQUEST_SCOPE);
105 
106         // Skip the body of this tag (if any)
107         return (SKIP_BODY);
108 
109     }
110 
111 
112     /**
113      * <p>Release any resources allocated by this tag instance.</p>
114      */
115     public void release() {
116 
117         this.messages = null;
118         this.var = null;
119 
120     }
121 
122 
123 }