What is the springMacroRequestContext called in the spring.ftl binding macro?

Springframework can integrate with the freemarker templating engine. Spring provides spring.ftl with basic macros and functions to facilitate form maintenance. To call a macro that creates a form field for you and also allows value selection (even for singleSelects or multiSelects).

The important component that the myth makes is its binding macro, which calls some mysterious springMacroRequestContext to get information about the binding status.

Where the hell does this variable come from and what's behind it?

+3


source to share


2 answers


Responsible for this is the class org.springframework.web.servlet.view.AbstractTemplateView

provided spring-mvc.*.jar

- line 154 / method renderMergedOutputModel(.*)

.

The template variable is hard-coded. I found out that next to some other ecological material.

When using springMacroRequestContext, we are talking about an instance org.springframework.web.servlet.support.RequestContext

.



The bind call creates an instance org.springframework.web.servlet.support.BindStatus

.

I do not write a blog. But there might be people like me looking for this to understand what's going on :)

+5


source


After investigating, the following sequence of procedures was found to manually instantiate an instance:

String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext";
Map<String, Object> model = new HashMap();
model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
                    new RequestContext(request, /*response, */
                                            /*request.getSession().getServletContext(),*/
                                            /*getServletContext()*/ model));

      

and later

Template t = freemarkerConfig.getConfiguration().getTemplate(templateName);
String text = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);

      



It will seamlessly provide the developer with access to common spring-s macros (e.g. spring.message, singleSelects, and others).

As you can see, you need to pass at least a non-null instance of the http servlet request.

Now I'm looking for a method to instantiate the springMacroRequestContext for the case of no http servlet request instance available.

Currently: I don't know "where the heck this variable is coming from" initially, because I haven't debugged or traced spring's own instances. However, I partially answered the second part of the question: "and what is hidden behind it", so in fact it is RequestContext();

.

0


source







All Articles