I am setting the data in model as:
@RequestMapping("/forms/builder/")
public void renderMethod1(Model model) {
SoyMapData x=new SoyMapData("class","menu horizontal right");
model.addAttribute("pageTitles", x);
}
Which is mapped to below function via Spring:
@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
String renderedResponse = null;
renderedResponse = compiledSoyTemplates.render(templateName, model, messageBundle);
}
Here I need to check if model is of type SoyMapData, if yes then set the type of model to SoyMapDataelse map.
How can I do this?
I have tried below approach its not working.
You can do that with the
instanceofoperator:...but if you need to do that, it suggests a problem with the encapsulation in the API design. Your
rendermethod shouldn't need to know what kind ofMapit's receiving, that breaks encapsulation.There are many ways to solve that. One is by deriving a new interface from
Mapthat allowsrenderto do its job, and then using that interface rather thanMapin the method signature. But that's only one way.Update: Re your updated code, if I understand you correctly, you want to call
compiledSoyTemplates.renderwith either aSoyDataModelargument or aMapargument depending on whethermodelis aSoyDataModel. (E.g., it's an overloaded method and you want the compiler to know which one to use.) If so, then you just use the information I gave you above like this: