JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » AJAX討論區 » ZK  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
己加入精華區
by CMHuang at 2007-01-08 13:41
本主題所含的標籤
無標籤
作者 zk的zul檔使用struts的resource bundle [精華]
jumperchen



版主

發文: 646
積分: 1
於 2007-01-07 14:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
由於zk提供的i3-label.properties好像沒辦法像struts用指定路徑的方式作設定,也沒辦法改變'i3-label'的prefix,在使用時也會因為zk不像struts是用ascii方式作編碼,所以在整合時會出無法跟現有系統的程式架構作整合。

整合步驟:
1、提供一個有function的tld檔,目前假設檔名用:zk-bean.tld,檔案路徑為:/WEB-INF/tlds/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
 
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
 
  <function>
    <name>message</name>
    <function-class>zk.taglib.functions.StrutsBean</function-class>
    <function-signature>java.lang.String message(java.lang.String,javax.servlet.jsp.PageContext)</function-signature>
    <example>
    ${bean:message("KEY:change.title;SCOPE:request",pageContext)}
    </example>
  </function>
</taglib>


2、提供實作的zk.taglib.functions.StrutsBean,
此程式是參考struts的org.apache.struts.taglib.bean.MessageTag改寫的,
當然使用的方式也是我假定的,程式最重要的地方為findMessage()改寫doStartTag(),將TagUtils.getInstance().write(pageContext, message);拿掉,改為return message;。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package zk.taglib.functions;
 
import java.util.Locale;
 
import javax.servlet.jsp.PageContext;
 
import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.util.MessageResources;
 
public class StrutsBean {
  private final static String KEY = "KEY:";
 
  private final static String ARG0 = "ARG0:";
 
  private final static String ARG1 = "ARG1:";
 
  private final static String ARG2 = "ARG2:";
 
  private final static String ARG3 = "ARG3:";
 
  private final static String ARG4 = "ARG4:";
 
  private final static String BUNDLE = "BUNDLE:";
 
  private final static String NAME = "NAME:";
 
  private final static String PROPERTY = "PROPERTY:";
 
  private final static String SCOPE = "SCOPE:";
 
  private final static String SEPARATE = ";";
 
  protected static MessageResources messages = MessageResources
    .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
 
  /**
   * The first optional argument.
   */
  private String arg0 = null;
 
  /**
   * The second optional argument.
   */
  private String arg1 = null;
 
  /**
   * The third optional argument.
   */
  private String arg2 = null;
 
  /**
   * The fourth optional argument.
   */
  private String arg3 = null;
 
  /**
   * The fifth optional argument.
   */
  private String arg4 = null;
 
  /**
   * The servlet context attribute key for our resources.
   */
  private String bundle = null;
 
  /**
   * The message key of the message to be retrieved.
   */
  private String key = null;
 
  /**
   * Name of the bean that contains the message key.
   */
  private String name = null;
 
  /**
   * Name of the property to be accessed on the specified bean.
   */
  private String property = null;
 
  /**
   * The scope to be searched to retrieve the specified bean.
   */
  private String scope = null;
 
  /**
   * The session scope key under which our Locale is stored.
   */
  private String localeKey = Globals.LOCALE_KEY;
 
  private static synchronized StrutsBean getStrutsBean(String src) {
    if (src == null || src.trim().equals("")) {
      return null;
    }
    StrutsBean sb = new StrutsBean();
    sb.key = substring(src,KEY);
    sb.arg0 = substring(src,ARG0);
    sb.arg1 = substring(src,ARG1);
    sb.arg2 = substring(src,ARG2);
    sb.arg3 = substring(src,ARG3);
    sb.arg4 = substring(src,ARG4);
    sb.bundle = substring(src,BUNDLE);
    sb.name = substring(src,NAME);
    sb.property = substring(src,PROPERTY);
    sb.scope = substring(src,SCOPE);
    return sb;
  }
  private static String substring(String src,String key){
    if (src.indexOf(key) != -1) {
      return src.substring(src.indexOf(key) + key.length(), (src
        .indexOf(SEPARATE,src.indexOf(key)) != -1 ? src.indexOf(SEPARATE, src
        .indexOf(key)) : src.length()));
    }else{
      return null;
    }
  }
  public static String message(String src, PageContext pageContext) {
 
    StrutsBean bean = getStrutsBean(src);
    if (bean != null) {
      try {
        return bean.findMessage(pageContext);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return "";
  }
 
  private String findMessage(PageContext pageContext) throws Exception {
    String key = this.key;
 
    if (key == null) {
      // Look up the requested property value
      Object value = TagUtils.getInstance().lookup(pageContext,
        this.name, this.property, this.scope);
 
      if ((value != null) && !(value instanceof String)) {
        Exception e = new Exception(messages.getMessage(
          "message.property", key));
 
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
      }
 
      key = (String) value;
    }
 
    // Construct the optional arguments array we will be using
    Object[] args = new Object[] { this.arg0, this.arg1, this.arg2,
        this.arg3, this.arg4 };
 
    // Retrieve the message string we are looking for
    String message = TagUtils.getInstance().message(pageContext,
      this.bundle, this.localeKey, key, args);
 
    if (message == null) {
      Locale locale = TagUtils.getInstance().getUserLocale(pageContext,
        this.localeKey);
      String localeVal = (locale == null) ? "default locale" : locale
        .toString();
      Exception e = new Exception(messages.getMessage("message.message",
        "\"" + key + "\"", "\""
            + ((this.bundle == null) ? "(default bundle)"
                : this.bundle) + "\"", localeVal));
 
      TagUtils.getInstance().saveException(pageContext, e);
      throw e;
    }
 
    return message;
  }
}
 


3、在xxx.zul檔的使用。('KEY:xxx' 等於 'struts-bean key="xxx"')
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<?taglib uri="/WEB-INF/tlds/zk-bean.tld" prefix="bean" ?>
<zk>
${bean:message("KEY:change.title;SCOPE:request",pageContext)}
</zk>


PS:
目前還沒想到更好解決zul使用resource bundle跟整合舊系統struts的resource bundle方法,以上的作法提供大家參考。

程式碼沒註解多多包含。(剛剛加加減減產出的結果)


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
8333 [精華] zk的zul檔使用struts的resource bundle jumperchen 5913 2007-01-07 14:52
1342 Re:zk的zul檔使用struts的resource bundle relifeted 314 2010-05-08 19:50
1264 Re:zk的zul檔使用struts的resource bundle jumperchen 88 2010-05-08 22:15
1626 Re:zk的zul檔使用struts的resource bundle relifeted 1918 2010-05-08 23:01
» JWorld@TW »  AJAX討論區 » ZK

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8