The premise: A little, SPRINGMVC knowledge
#springMessage ("xxx") to deal with internationalization:
The way Spring handles internationalization can be very special , because this tag can be used.
This tag is used only after spring has integrated the velocity template. This template is available in:
Org.springframework.web.servlet\src\main\java\org\springframework\web\servlet\view\velocity\spring.vm
The first macro in the velocity template is the springmessage tag, and there are other markers, which are really handy if you use velocity.
#macro (Springmessage $code) $springMacroRequestContext. GetMessage ($code) #end
There are doubts here. Because this macro used to $springmacrorequestcontext this thing and what's going on?
It turns out that this variable is defined and assigned in Org.springframework.web.servlet.view.AbstractTemplateView.java.
Public abstract class Abstracttemplateview extends Abstracturlbasedview {
/**
* Variable Name of the RequestContext instance in the template model,
* Available to Spring's macros:e.g. For creating Bindstatus objects.
*/
public static final String Spring_macro_request_context_attribute = "Springmacrorequestcontext";
//... Slightly
Can literally be understood, rendering the macro and outputting it to the model
Protected final void Rendermergedoutputmodel (
Map<string, object> model, httpservletrequest request, httpservletresponse response) throws Exception {
If this property is set to True when configuring the Velocityviewresolver template, the attribute values in the request will be placed in the model
if (this.exposerequestattributes) {
For (Enumeration en = Request.getattributenames (); en.hasmoreelements ();) {
string attribute = (string) en.nextelement ();
if (Model.containskey (attribute) &&!this.allowrequestoverride) {
throw new Servletexception ("Cannot expose request attribute '" + attribute +
"' Because of an existing model object of the same name");
}
Object AttributeValue = Request.getattribute (attribute);
if (logger.isdebugenabled ()) {
Logger.debug ("Exposing request Attribute" + attribute +
"' with value [" + AttributeValue + "] to model");
}
Model.put (attribute, AttributeValue);
}
}
If this property is set to True when configuring the Velocityviewresolver template, the attribute values in the session will be placed in the model
if (this.exposesessionattributes) {
HttpSession session = Request.getsession (false);
if (session! = NULL) {
For (Enumeration en = Session.getattributenames (); en.hasmoreelements ();) {
string attribute = (string) en.nextelement ();
if (Model.containskey (attribute) &&!this.allowsessionoverride) {
throw new Servletexception ("Cannot expose session attribute '" + attribute +
"' Because of an existing model object of the same name");
}
Object AttributeValue = Session.getattribute (attribute);
if (logger.isdebugenabled ()) {
Logger.debug ("Exposing session Attribute" + attribute +
"' with value [" + AttributeValue + "] to model");
}
Model.put (attribute, AttributeValue);
}
}
}
If the macro can be used, from Velocityviewresolver's parent class Abstracttemplateviewresolver
You can learn that exposespringmacrohelpers defaults to True
if (this.exposespringmacrohelpers) {
If the model exists, then an exception is thrown, otherwise
if (Model.containskey (Spring_macro_request_context_attribute)) {
throw New Servletexception (
"Cannot expose bind macro helper" + Spring_macro_request_context_attribute +
"' Because of an existing model object of the same name");
}
Expose RequestContext instance for Spring macros.
The RequestContext instance object is exposed as a macro variable, which is directly assigned to the Spring_macro_request_context_attribute.
Model.put (Spring_macro_request_context_attribute,
New RequestContext (Request, Response, Getservletcontext (), model));
}
Applycontenttype (response);
Rendermergedtemplatemodel (model, request, response);//render velocity template, subclass Velocityview will implement
}
This allows you to use #springmessage ("xxx") in the page to get the code value of the resource. Of course, you can see in the code that this macro is the RequestContext object.
Use it flexibly.
#springMessage宏的根源