2

I have a string (user message) and I would like to find (using regexp) special codes in it and replace them with links. (For example, @user will be replaced with <a href="wicket-url-to-user-profile">user</a> thing.) How can it be done in the Wicket? I do not ask about regular expressions, it is Wicket-focused question.

8
  • Where would those links point? Within the application or to an external url?
    – biziclop
    Jun 16, 2011 at 13:19
  • Not sure what part you are asking about. The whole thing? Anyway, here is a good link for stackoverflow.com/questions/4234950/regex-for-beginners.
    – lindon fox
    Jun 16, 2011 at 13:20
  • Regexp is easy part. I'm asking about Wicket-part.
    – javap
    Jun 16, 2011 at 13:21
  • 1
    Can you give an example of what you need? And which part you're unsure about.
    – biziclop
    Jun 16, 2011 at 13:22
  • For example, @user will be replaced with <a href="wicket-url-to-user-profile">user</a> thing.
    – javap
    Jun 16, 2011 at 13:24

2 Answers 2

4

In fact, Wicket has a very powerful feature for this.

Let's say this is your localization string with the key "some.resource.key":

This is a ${user}.

Then you add the following to your Wicket template:

<wicket:message key="some.resource.key">
    <a wicket:id="user">user</a>
</wicket:message>

In your Wicket page (or Panel), simply add your link as if you were adding it to the level where you put the wicket:message:

add(new Link("user"));

Now the link - you can use whatever implementation of link or any other component you like - is displayed correctly embedded in your localized string.

2
  • As I said, text is some user message. We do not know what this text is, and if it contains any codes. User does not know syntax ${user_nick}, text can contain syntax errors. And, so you need to define <a wicket:id="user">user</a> for every user nick?
    – javap
    Jun 16, 2011 at 16:12
  • Than this in fact is not a (purely) Wicket-related question. Find the string to replace using a regexp, extract the username, find the user and replace the string with a link. Easy as that. To get the correct URL to a Wicket page, you can use any component's urlFor() method.
    – Lunikon
    Jun 16, 2011 at 16:27
1

Wicket has IResponseFilter. With this you can post-process the final HTML. I.e. you can search for ${anything} and replace it with 'whatever you want'. To create nice looking URLs I suggest you to mount specific pages for the specific domain objects, e.g. Application#mountPage("/users/${user}", UsersPage.class), and create the urls with : RequestCycle.get().urlFor(UsersPage.class, pageParameters), where 'pageParameters' contains an entry with key 'user' and value 'whatever you extracted with RegEx'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.