2012-07-09 16 views
3

如何爲我的表單組件正確創建國際化標籤,以便在顯示反饋消息時顯示國際化字段名稱而不是java代碼中字段的名稱?Wicket中表單組件的國際化標籤

我讀過這樣的:

https://cwiki.apache.org/WICKET/everything-about-wicket-internationalization.html

以及檢票的XHTML標籤的文檔:

https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html

<label wicket:for="name"> 
    <wicket:label> 
     <wicket:message key="label.name"/> 
    </wicket:label> 
</label> 
<input wicket:id="name" type="text" wicket:message="placeholder:label.name" /> 

這將導致以下錯誤:

Last cause: Expected close tag for '<wicket:label>' Possible attempt to embed 
component(s) '<wicket:message key="label.name"/>' in the body of this 
component which discards its body 

如果我用一些任意文本替換wicket:message,它會在任何相關的反饋消息中顯示文本。

(有一個相關的JIRA問題:https://issues.apache.org/jira/browse/WICKET-3903但我還是不明白,做了什麼來解決這個問題,什麼我必須做的......)

剛剛發現有一種方法,在java中做到這一點:

add(new TextField<String>("name").setRequired(true).setLabel(new Model<String>(getString("label.name")))); 

是否有可能以某種方式以更舒適的方式做到這一點?

回答

4

我剛剛測試了以下:

<form wicket:id="form"> 
     <label for="input"><wicket:message key="input">some input</wicket:message></label> 
     <input wicket:id="input" type="text" name="input"> 
     <input type="submit" value="submit"> 
</form> 

並在Java類:

Form<HomePage> form = new Form<HomePage>("form" 
           , new CompoundPropertyModel<HomePage>(this)); 
    wmc.add(form); 

    TextField textField = new TextField("input"); 
    textField.setRequired(true); 
    form.add(textField); 

在我所提供的屬性文件:

input=SomeInputField 

這導致了下面的屏幕(如果我將請求的字段留空並按提交。

enter image description here

這是你在找什麼?

+0

啊所以訣竅是命名標籤完全相同的字段沒有李我做的「label.name」不會工作「名稱」,但是正在爲字符串資源工作。 thx :) – Yashima 2012-07-10 13:13:44

3

這裏是一個替代方法@bert's這一直爲我工作(不知道的<wicket:label>

FormComponent所示,當發生時通過的FormComponent.setLabel(IModel)來指定驗證錯誤的文本。顯示的文本將是IModelgetObject()的結果。

TextField comp = new TextField("comp"); 
// Use internationalized text from XML resource file 
comp.setLabel(new StringResourceModel("formResources.comp.label", this, null)); 

聲明本無關與<label>也不FormComponentLabelFormComponentLabel是一個可用於對<label>標籤進行建模的組件。

你甚至可以子類FormComponentLabel提供基於FormComponent.getLabel()標籤文本,也許輸出時,​​需要該領域的額外標誌:

public class MyLabel extends SimpleFormComponentLabel{ 
    private boolean required; 
    public MyLabel (String id, LabeledWebMarkupContainer labelProvider) { 
    super(id, labelProvider); 
      if (labelProvider instanceof FormComponent){ 
       required = ((FormComponent)labelProvider).isRequired(); 
     } 
    } 

    protected void onComponentTagBody(final MarkupStream markupStream, 
             final ComponentTag openTag) { 
     String mark = ""; 
     if (required){ 
      // could be for instance "*" 
      mark = getString("formResources.requiredField"); 
     } 
     String text = getModelObjectAsString() + mark; 
     replaceComponentTagBody(markupStream, openTag, text); 
    } 
} 

{ 
TextField component = new TextField("component"); 
component.setRequired(true); 
component.setOutputMarkupId(true); 
IModel labelModel = new StringResourceModel("formResources.component.label", 
              this, null); 
component.setLabel(labelModel); 
add(component); 
add(new MyLabel("componentLabel", component); 
} 

<label wicket:id="componentLabel"/> 
<input type="text" wicket:id="component"/> 

這樣,您將有

    清潔方式
  1. FormComponent的文本設置爲國際化資源字符串
  2. <label> t重複使用完全相同的資源字符串並根據FormComponent的屬性添加自定義標記。
+0

我已經知道setLabel()方法,它確實工作得很好。我正在尋找上面更簡單的方法,它不那麼冗長 - 在java中。一旦我的應用程序變得更加複雜,但我很確定我會回來看看你的方法。謝謝! – Yashima 2012-07-10 13:19:09

+0

有趣的解決方案。我會繼續在腦海中爲進一步的參考。 – bert 2012-07-10 19:21:49

3

另一種方法是使用<wicket:label/>的關鍵屬性,就像這樣:

<label wicket:for="name"> 
    <wicket:label key="label.name">Placeholder label</wicket:label> 
</label> 
<input wicket:id="name" type="text"/> 

不幸的是這個屬性沒有文檔描述檢票口的XHTML標籤的wiki頁面。在處理標籤的類中使用JavaDoc記錄所有支持的屬性(org.apache.wicket.markup.html.form.AutoLabelTextResolver)。

這種替代方法的優點是不需要額外的編碼。

1

Wicket拋出一個例外,告訴您將刪除您的<wicket:message>標籤,因爲<wicket:label>標籤的主體被替換。問題是你不能在<wicket:label>標籤內嵌套<wicket:message>標籤(並且不需要)。

無論這個(選項1):

<label wicket:for="name"> 
    <wicket:label key="label.name"/> 
</label> 
<input wicket:id="name" type="text /> 

或本(選項2):

<label wicket:for="name"> 
    <wicket:message key="label.name"/> 
</label> 
<input wicket:id="name" type="text /> 

應該爲你工作,並導致HTML類似如下(假設屬性文件包含label.name=Name):

<label for="someMarkupId"> 
    Name 
</label> 
<input id="someMarkupId" type="text" /> 

所不同的是,如果你設置l阿貝爾對於像這樣通過Java代碼的組件:使用選項1將導致標籤

component.setLabel(new Model("value set in code")); 

然後被設置爲「值在代碼中設置」,而使用選項2仍然會導致標籤設置爲「名稱」。另外,如果標籤是通過Java代碼設置的,並且屬性文件中缺少密鑰,則選項2將引發異常,而選項1將僅使用代碼中設置的值。

0

我更喜歡這樣的:

<label wicket:for="name"><wicket:label />:</label> 
<input type="text" wicket:id="name"></input> 

只要確保設置使用setLabelFormComponent的標籤,因此唯一需要的就是java:

add(new TextField("name", nameModel).setLabel(Model.of("i18n.name")));  

這將呈現爲(在荷蘭):

<label id="name63-w-lbl" for="name63">Naam:</label> 
<input type="text" value="" name="name" id="name63">