#springMessage () macro's doubts

Source: Internet
Author: User

#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 Model protected final void rendermergedoutputmodel (     Map<String, Object> model, HttpServletRequest request,  Httpservletresponse response)  throws exception {  // If you set this property to True when you configure the Velocityviewresolver template, the attribute values in the request are 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 you set this property 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 the Velocityviewresolver parent class Abstracttemplateviewresolver    //to know 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.     //  Translate: Exposing an RequestContext instance object to a macro variable, which is directly assigning a value to 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 () macro's doubts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.