SPRINGMVC Dispatchservlet initialization of nine load strategies (i)

Source: Internet
Author: User

When the SPRINGMVC container is initialized,

protected void Onrefresh (ApplicationContext context) {    this.initstrategies (context);} protected void Initstrategies (ApplicationContext context) {    this.initmultipartresolver (context);    This.initlocaleresolver (context);    This.initthemeresolver (context);    This.inithandlermappings (context);    This.inithandleradapters (context);    This.inithandlerexceptionresolvers (context);    This.initrequesttoviewnametranslator (context);    This.initviewresolvers (context);    This.initflashmapmanager (context);}

 

1. initmultipartresolver File Upload

Source:

This.multipartresolver = (multipartresolver) context.getbean ("Multipartresolver", Multipartresolver.class);

The Multipartresolver interface is defined as follows:

Public interface Multipartresolver {    //check if the request header contains a file stream upload    boolean ismultipart (HttpServletRequest var1);        File stream upload request parsing method, parsed and encapsulated in the Multiparthttpservletrequest object    multiparthttpservletrequest Resolvemultipart ( HttpServletRequest var1) throws multipartexception;    void Cleanupmultipart (Multiparthttpservletrequest var1);}

We know that the corresponding request will be intercepted by the Dispatchservlet Dodispatch method, the Dodispatch method will first call the Checkmultipart method to check whether the request is a file stream:

Protected HttpServletRequest Checkmultipart (HttpServletRequest request) throws Multipartexception {        if ( This.multipartresolver! = null && this.multipartResolver.isMultipart (Request)) {            if ( Webutils.getnativerequest (Request, multiparthttpservletrequest.class)! = null) {                this.logger.debug ("...");            } else if (this.hasmultipartexception (Request)) {                This.logger.debug ("...");            } else {                try {                    //specific file stream parsing method                    return This.multipartResolver.resolveMultipart (request);                    ...
}
Configuration examples

Commonsfileuploadsupport implements the Multipartresolver interface

Upload file configuration @bean (name = "Multipartresolver") public Commonsfileuploadsupport Commonsfileuploadsupport () {    Commonsfileuploadsupport resolver = new Commonsmultipartresolver ();    Resolver.setmaxinmemorysize (40960);    Resolver.setmaxuploadsize (10485760000L);    return resolver;}

  

2. Initlocaleresolver Internationalization

Dispatcherservlet.properties file:

Org.springframework.web.servlet.localeresolver= Org.springframework.web.servlet.i18n.acceptheaderlocaleresolverorg.springframework.web.servlet.themeresolver= org.springframework.web.servlet.theme.fixedthemeresolverorg.springframework.web.servlet.handlermapping= Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping, Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingorg.springframework.web.servlet.HandlerAdap Ter=org.springframework.web.servlet.mvc.httprequesthandleradapter, Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter, Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterorg.springframework.web.servlet.HandlerExcep Tionresolver=org.springframework.web.servlet.mvc.annotation.annotationmethodhandlerexceptionresolver, Org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver, Org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolverorg.springframework.web.servlet.RequestToViewNametranslator= Org.springframework.web.servlet.view.DefaultRequestToViewNameTranslatororg.springframework.web.servlet.ViewResolver =org.springframework.web.servlet.view.internalresourceviewresolverorg.springframework.web.servlet.flashmapmanager =org.springframework.web.servlet.support.sessionflashmapmanager

A static container is defined in Dispatchservlet to initialize the default Defaultstrategies method block, and if the user is not customized, the default is used,

static {    //Load default strategy implementations from properties file.    This is currently strictly internal, and not meant to being customized    //By application developers.    try {        Classpathresource resource = new Classpathresource (Default_strategies_path, dispatcherservlet.class);        Defaultstrategies = propertiesloaderutils.loadproperties (Resource);    }    catch (IOException ex) {        throw new IllegalStateException ("Could not load '" +     Default_strategies_path + "':" + ex . GetMessage ());}    }

Process:

1. This.localeresolver = Getdefaultstrategy (context, localeresolver.class); 2. List<t> strategies = getdefaultstrategies (Context, Strategyinterface) 3. String key = Strategyinterface.getname ();   String value = Defaultstrategies.getproperty (key);   If value is not NULL, the value is converted to the class class name, and then the object is created in the container   strategy = this.createdefaultstrategy (context, clazz);

PRINGMVC Internationalization provides four internationalized implementations of the class Acceptheaderlocaleresolver (default), Fixedlocaleresolver, Cookielocaleresolver and Sessionlocaleresolver.

3. Initthemeresolver Theme

The theme of the implementation principle: probably is the site layout of CSS style sheets and pictures and other files and the program of the site to decouple, the program read theme persistent configuration, and then find the corresponding CSS style sheets and pictures, configure the site layout.

To use a theme in your program, you must set up an org.springframework.ui.context.ThemeSource implementation class. The SPRINGMVC IOC container itself implements the Themesource, which simply assigns the responsibility to a particular implementation class, which, by default, is: org.springframework.ui.context.support.ResourceBundleThemeSource This implementation class can be downloaded from the Classpath directory into a properties file. such as setting Setbasenameprefix, setdefaultencoding and so on.

Principle (Practice)

For example, a set of topics in SPRINGMVC corresponds to a cool.properties file, which, under the Classpath root directory, lists resources that are composed of topics:

Stylesheet=/themes/cool/style.cssbackground=/themes/cool/img/coolbg.jpg

The key in the properties file refers to the name of the element in the view file, and the location where the value topic is stored. JSP files can be used to access the key through Spring:theme, and then find the value (corresponding topic).

The role of resourcebundlethemesourceuses (the default themesource) is to find specific topics based on the subject name, In this example, the configuration file themedemo.properties is found, by default Resourcebundlethemesource uses an empty prefix name, so the properties file in the Classpath root directory is loaded. If you want to make a location, you can use it basenamePrefix .

After finding the topic file, how to parse? At this point there is a themeresolver.

There are 3 implementation theme classes in SPRINGMVC:

    1. Fixedthemeresolver (default): fixed-format theme, which cannot dynamically change theme while the system is running.

    2. Sessionthemeresolver: You can dynamically adjust the value of theme by changing the corresponding key value in the cookie in the run.

    3. Cookiethemeresolver: You can dynamically adjust the value of theme by changing the corresponding key value in the session during Operation

The topic load policy is similar to Initlocaleresolver, and is the default if the user does not customize it. The default mode is Fixedthemeresolver.

An example:

Application context. XML configuration file:

<bean class= "Org.springframework.ui.context.support.ResourceBundleThemeSource" id= "Themesource" >      < Property Name= "Basenameprefix" value= "themes/" ></property>  </bean>  <bean id= " Themeresolver "  class=" Org.springframework.web.servlet.theme.SessionThemeResolver ">      <property Name = "Defaultthemename" value= "Red"/>  </bean>

The simple narrative is: Themeresolver finds all properties in the Themes/directory, and then sessionthemeresolver resolves the topic after the subject request has changed.

Attached: If you need to change the theme based on user requests, you need to use the Themechangeinterceptor interceptor to change the theme. (Interceptor-related knowledge can be seen in the handlermapping below)

Reference:

Https://www.xuebuyuan.com/zh-tw/1578077.html

52915063

SPRINGMVC Dispatchservlet initialization of nine load strategies (i)

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.