How to inject values to a wicket page using spring?
Asked Answered
S

2

6

Is it possible to inject a value to a wicket page using spring?

With @Value it's possible to inject values to a spring bean.

I know the @SpringBean annotation, but this is only for beans.

My workaround is to wrap the value with a spring bean and then inject this with @SpringBean to my wicket page. Is there a better way to do this?

Stonework answered 7/10, 2014 at 9:41 Comment(2)
Yes, you can use @Value to inject values.Podgorica
@CodeFreak: Yes, to inject into spring beans, but not into a wicket page, like i asked.Stonework
H
1

We have solved this problem using getter & setter in our custom child of WebApplication. This child is standard Spring bean and is configured in spring's configuration.

Otherwise you have to create some "config" bean.

Hexapod answered 16/3, 2015 at 15:6 Comment(0)
L
0

You can write a Wicket resource loader to load spring values, and then those values will be resolved like regular wicket messages. If instead you need it within the body of the wicket class to do some business logic, that may be an opportunity to refactor that logic outside of the view layer.

Here's what the resource loader looks like:

public class SpringPropertiesResourceLoader
    implements IStringResourceLoader
{

    public SpringPropertiesResourceLoader()
    {

    }

    @Override
    public String loadStringResource(Class<?> clazz, String key, Locale locale, String style, String variation)
    {
        return loadStringResource(key);
    }

    @Override
    public String loadStringResource(Component component, String key, Locale locale, String style, String variation)
    {
        return loadStringResource(key);
    }

    private String loadStringResource(String key)
    {
        try
        {
            ApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(WebPortalApplication.get().getServletContext());
            ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory)applicationContext.getAutowireCapableBeanFactory();
            String rv = beanFactory.resolveEmbeddedValue("${" + key + "}");
            return rv;
        }
        catch (IllegalArgumentException iae)
        {
            // no property with the name - move along
            return null;
        }
    }

}

Then add that to your application in init():

    getResourceSettings().getStringResourceLoaders().add(new SpringPropertiesResourceLoader());
Labbe answered 13/10, 2014 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.