I will answer my own post, just in case somebody else is looking for a
solution to the same problem. I have found two ways to get the resource
loading to do exactly what I want (There are probably a lot more out there):

Solution 1 (The easiest one in my opinion):
Do the following changes to ContactPanel.html:

Replace the <span wicket:id="nameLabel"></span> tag with <wicket:message
key="nameLabel"></wicket:message> (and the same for userNameLabel,
phoneLabel and emailLabel).

It works straight away for the nameLabel in the hrContactPanel it picks up
the string "HR Contact:" from the VacancyPage.properties file and for the
nameLabel in hiringManagerPanel it picks up the string "Hiring Manager:"
instead, just as it should :-)

Solution 2:
Instead of using the ResourceModel you have to use the StringResourceModel
and supply the component and an empty model. Note that it does not work
unless you supply the component (this) to the ResourceModel. Here is the
updated ContactPanel.java:

public class ContactPanel extends Panel {
    private static final long serialVersionUID = 1L;

    public ContactPanel(String id, IModel model) {
        super(id, model);
        add(new Label("nameLabel", new StringResourceModel("nameLabel",
this, null)));
        add(new Label("name"));

        add(new Label("userNameLabel", new
StringResourceModel("userNameLabel", this, null)));
        add(new Label("userName"));

        add(new Label("phoneLabel", new StringResourceModel("phoneLabel",
this, null)));
        add(new Label("phone"));

        add(new Label("emailLabel", new StringResourceModel("emailLabel",
this, null)));
        add(new Label("email"));
    }
}

Best Regards,
/Peter

2008/6/11 Peter Eriksson <[EMAIL PROTECTED]>:

>
> HTML for the panel ((ContactPanel.html):
> <html xmlns:wicket>
> <wicket:panel>
>     <table>
>         <tr>
>             <td><span wicket:id="nameLabel"></span></td>
>             <td><span wicket:id="name"></span></td>
>         </tr>
>         <tr>
>             <td><span wicket:id="userNameLabel"></span></td>
>             <td><span wicket:id="userName"></span></td>
>         </tr>
>         <tr>
>             <td><span wicket:id="phoneLabel"></span></td>
>             <td><span wicket:id="phone"></span></td>
>         </tr>
>         <tr>
>             <td><span wicket:id="emailLabel"></span></td>
>             <td><span wicket:id="email"></span></td>
>         </tr>
>     </table>
> </wicket:panel>
> </html>
>
> Java code for the panel (ContactPanel.java):
> public class ContactPanel extends Panel {
>     private static final long serialVersionUID = 1L;
>
>     public ContactPanel(String id, IModel model) {
>         super(id, model);
>         add(new Label("nameLabel", new ResourceModel("nameLabel")));
>         add(new Label("name"));
>
>         add(new Label("userNameLabel", new
> ResourceModel("userNameLabel")));
>         add(new Label("userName"));
>
>         add(new Label("phoneLabel", new ResourceModel("phoneLabel")));
>         add(new Label("phone"));
>
>         add(new Label("emailLabel", new ResourceModel("emailLabel")));
>         add(new Label("email"));
>     }
> }
>
>
> HTML for the page using the panel (VacanyPage.html):
> --- snip ---
>                     <table>
>                         <tr>
>                             <td><span
> wicket:id="hrContactPanel"></span></td>
>                         </tr>
>                         <tr>
>                             <td><span
> wicket:id="hiringManagerPanel"></span></td>
>                         </tr>
>                     </table>
> --- snip ---
>
> Java code for the page (VacanyPage.java):
> --- snip ---
>         Form form = new Form("vacancyForm", new
> CompoundPropertyModel(vacancyReq)) {
> --- snip ---
>         Contact hrContact = new Contact();
>         form.add(new ContactPanel("hrContactPanel", new
> CompoundPropertyModel(hrContact)));
>         Contact hiringManager = new Contact();
>         form.add(new ContactPanel("hiringManagerPanel", new
> CompoundPropertyModel(hiringManager)));
> --- snip ---
>
> Properties file for the page (VacancyPage.properties):
> vacancyForm.hrContactPanel.nameLabel=HR Contact:
> vacancyForm.hrContactPanel.userNameLabel=HR Contact Username:
> vacancyForm.hrContactPanel.phoneLabel=Phone:
> vacancyForm.hrContactPanel.emailLabel=Email:
> vacancyForm.hiringManagerPanel.nameLabel=Hiring Manager:
> vacancyForm.hiringManagerPanel.userNameLabel=Hiring Manager Username:
> vacancyForm.hiringManagerPanel.phoneLabel=Phone:
> vacancyForm.hiringManagerPanel.emailLabel=Email:
>
>
> As you can see from the code the VacancyPage has two instances of
> ContactPanel and these should have different labels. I have tried different
> ways to specify this, but Wicket says it cannot find the labels, so I must
> be doing something wrong.
>
> Thanks in advance for any help!
>
> Best Regards,
> /Peter
>
> 2008/6/11 Martin Makundi <[EMAIL PROTECTED]>:
>
> Can you paste the code for better insight into your problem?
>>
>> 2008/6/11 Peter Eriksson <[EMAIL PROTECTED]>:
>> > Hello,
>> >
>> > I have just begun to use wicket and so far I am very impressed with the
>> > framework.
>> > I have a question about best practice to solve a problem. I have a
>> component
>> > (wicket panel) that contain contact information (like name, address,
>> email,
>> > phone, etc) and on a specific web page I want to have two of the contact
>> > panels. They of course access different model fields. Now to the
>> question:
>> > The labels used for the contact fields are slightly different, so the
>> > question is how to best solve this? Since the labels are not the same
>> for
>> > the two contact panels I cannot use a ResourceModel as it is because
>> then
>> > they would have the same label text. Is the best way to solve this with
>> > inheritence or do you have any other solution?
>> >
>> > Best Regards,
>> > /Peter
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>

Reply via email to