0

Within my web application I have a tool for generating PDF/XLS reports. These "reporters" inherit from a basic controller and they just describe the reporting functionality, something like:

public abstract class Reporter {

    getPath() {
        return "/reporter/" + getClass().getSimpleName().replace("Reporter", "").toLowerCase() + ".{pdf|xls}";
    }        

    handleRequest() {
        // prepare the data
        generateReport(...)
        // do something with it
        // then report pdf or excel
    }
}

@Controller
public class DailyReporter extends Reporter {
    @Override
    void generateReport(...) {}
}

@Controller
public class AverageReporter extends Reporter {
    @Override
    void generateReport(...) {}
}

In this way I can just describe the data for each Reporter.

Using Spring MVC, the getPath() method is actually part of the Annotation, but using getClass().getSimpleName().toLowerCase() in the annotation is not possible as it needs to be "Compile time constant". Is there a way to do this with Spring MVC?

1 Answer 1

2

Don't use inheritance. Use composition/delegation.

Have a single controller, mapped to /reporter/{type}.{pdf|xls}. Define an interface Reporter, and one spring bean implementing that interface per type of report. Inject a List<Reporter> in your controller. And for each request, find the reporter responsible for the type passed in the URL, and call it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.