Question

I have several webpages with similar forms on them.

One field that exists in several of the pages are email-address.

I want to be able to use a page specific message code, but I would like to be able to reference another message code in order to have a single declaration. In this way, I can change the look of the email-adress label one place and have it changed in all the webpages, but at the same time, I'm able to change the text for a single page with only propertyfile updates.

I'm looking for functionality like this:

message.properties:

label.email=Email address

webpage1.label.email=${label.email}
webpage2.label.email=${label.email}

However, when using the following jsp-code:

<spring:message code="webpage1.label.email"/>

I get the literal ${label.email} instead of "Email address" in my webpages.

Any hints?

No correct solution

OTHER TIPS

You can replace the DefaultPropertiesPersister with this one:

This will allow you to reference other entries, e.g.:

user=User
user.add=Add ${user}
user.delete=Delete ${user}

Simply specify this persister with your MessageSource, e.g. messageSource.setPropertiesPersister(new RecursivePropertiesPersister());

Source:

public class RecursivePropertiesPersister extends DefaultPropertiesPersister {
    private final static Pattern PROP_PATTERN = Pattern.compile("\\$'?\\{'?([^}']*)'?\\}'?");
    private final static String CURRENT = "?LOOP?";

    @Override
    public void load(Properties props, Reader reader) throws IOException {
        Properties propsToLoad = new Properties();
        super.load(propsToLoad, reader);
        replace(propsToLoad, props);
    }

    @Override
    public void load(Properties props, InputStream is) throws IOException {
        Properties propsToLoad = new Properties();
        super.load(propsToLoad, is);
        replace(propsToLoad,props);
    }

    protected void replace ( Properties src, Properties dest) {
        for (Map.Entry entry: src.entrySet()) {
            String key = (String) entry.getKey();
            String value = (String)entry.getValue();
            replace(src, dest, key, value);
        }
    }

    protected String replace(Properties src, Properties dest, String key, String value) {
        String replaced = (String) dest.get(key);
        if (replaced != null) {
            // already replaced (or loop), just return the string
            return replaced;
        }
        dest.put(key,CURRENT); // prevent loops
        final Matcher matcher = PROP_PATTERN.matcher(value);
        final StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            final String subkey = matcher.group(1);
            final String replacement = (String)src.get(subkey);
            matcher.appendReplacement(sb,replace(src,dest,subkey,replacement));
        }
        matcher.appendTail(sb);
        final String resolved = sb.toString();
        dest.put(key, resolved);
        return resolved;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top