简体   繁体   中英

Java Freemarker templating engine access only custom annotated methods?

During encapsulation of view variables to a POJO class and supplying it to the template processor, is it possible to limit the methods that can be invoked by the templating engine with a custom annotation? Limiting the visibility via private method is not possible because of inheritance.

I am using FreeMarker with Spring MVC.

The template processor receives the POJO class from the controller:

model.addAttribute("view", new SamplePOJO(this));

The SamplePOJO class:

public final class SamplePOJO extends View
{
    public SamplePOJO(MasterController<?> _inject)
    {
        super(_inject);
    }

    @CustomAnnotationOnValidMethods
    public String getValid_value()
    {
        return "valid call";
    }

    @Override
    public String getInvalid_value()
    {
        return "invalid call";
    }
}

The template:

<p>Valid invocation: ${view.valid_value}</p>
<p>Also valid but I want this to be impossible: ${view.invalid_value}</p>

Yes, DefaultObjectWrapper (or any other BeansWrapper subclass) has a methodAppearanceFineTuner setting ( https://freemarker.apache.org/docs/api/freemarker/ext/beans/MethodAppearanceFineTuner.html ). In your MethodApperanceFineTuner implementation you can hide a method by calling MethodAppearanceDecision.setExposeMethodAs(null) . You can inject your MethodApperanceFineTuner object where you configure FreeMarker, like, you create a DefaultObjectWrapperBuilder , call its setMethodAppearanceFinteTuner(MethodAppearanceFinteTuner) method, then build() , and pass the result to Configuration.setObjectWrapper(ObjectWrapper) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM