SPRING in ACTION 4th edition notes-seventh Chapter advanced Spring mvc-001-dispatcherservlet Configuration (servletregistration.dynamic, Webapplicationinitializer)
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 18px;">One</span></p></p><p><p><span style="font-size: 18px;">1. If you want to customize some actions after Dispatcherservlet is registered in the servlet container, such as opening the file upload function, Can be overridden by Abstractannotationconfigdispatcherservletinitializer's Customizeregistration () to implement</span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">after <span style="color: #008000;">abstractannotation Configdispatcherservletinitializer Registers Dispatcherservlet</span> with the servlet</span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">container, It calls the Customizeregistration () method, passing in the servlet-</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">registration.dynamic that resulted from the servlet Registration. By overriding</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">customizeregistration (), You can apply additional configuration to Dispatcherservlet.</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">with the servletregistration.dynamic that's given to Customizeregistration (),</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">you <span style="color: #008000;">can does several things, including set the Load-on-startup priority by calling set-</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Loadonstartup (), set an initialization parameter by calling Setinitparameter (), and</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">call <span style="color: #008000;">setmultipartconfig () To configure Servlets 3.0 multipart Support.</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">customizeregistration (Dynamic Registration) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">Registration.setmultipartconfig (</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Multipartconfigelement ("/tmp/spittr/uploads", 2097152, 4194304, 0<span style="color: #000000;"><span style="color: #000000;">));</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><p><p><span style="font-size: 18px;">2. Register the servlet by resetting Webapplicationinitializer's Onstartup</span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.myapp.config;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">javax.servlet.ServletContext;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">javax.servlet.ServletException;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">javax.servlet.ServletRegistration.Dynamic;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.springframework.web.WebApplicationInitializer;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">com.myapp.MyServlet;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myservletinitializer<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">Webapplicationinitializer {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onstartup (servletcontext Servletcontext)</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">servletexception {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>Dynamic Myservlet =<span style="color: #008080;"><span style="color: #008080;"></span> a</span>Servletcontext.addservlet ("myservlet", Myservlet.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Myservlet.addmapping ("/custom/**"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>}</pre><p><p>is <span style="font-size: 18px;">a rather basic servlet-registering initializer class. It registers a servlet </span>and <span style="font-size: 18px;">maps it to a single path. You could the approach to register Dispatcherservlet </span> <span style="font-size: 18px;">manually. (but there ' s no need, because abstractannotationconfigdispatcher-</span><br><span style="font-size: 18px;">Servletinitializer does a fine job without as much code.)</span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">3. Register the filter by resetting Webapplicationinitializer's Onstartup</span></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">@Override</span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">onstartup (servletcontext Servletcontext)</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">throws</span> <span style="color: #000000;">servletexception {</span><span style="color: #008080;">4</span> javax.servlet.FilterRegistration.Dynamic filter = Servletcontext.addfilter ("myfilter", myfilter. <span style="color: #0000ff;"> Class</span><span style="color: #000000;">);</span> <span style="color: #008080;">5</span> Filter.addmappingforurlpatterns (<span style="color: #0000ff;">null</span><span style="color: #0000ff;">false</span>, "/custom/*"<span style="color: #000000;">);</span> <span style="color: #008080;">6</span> }</pre></pre><p><p></p></p><p><p><span style="font-size: 18px;">4.</span><span style="font-size: 18px;">to register one or more filters and maps them to dispatcherservlet, all of need to does is </span> <span style="font-size: 18px;">override The Getservletfilters () method of Abstractannotationconfig</span><em id="__mceDel" style="line-height: 1.5;"><em id="__mceDel"><em id="__mceDel"><span style="font-size: 18px;">dispatcherservletinitializer.</span></em></em></em></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">@Override</span> <span style="color: #008080;">2</span> <span style="color: #0000ff;">protected</span> <span style="color: #000000;">filter[] getservletfilters () {</span><span style="color: #008080;">3</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">New</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">myfilter ()};</span> <span style="color: #008080;">4</span> }</pre></pre><p><p><span style="font-size: 18px;">as can see, the This method returns an array of Javax.servlet.Filter. Here it </span> <span style="font-size: 18px;">is only returns a single filter and it could return as many filters as you need. There ' s no need </span>to declare, the mapping for the <span style="font-size: 18px;">filters, any filter returned from Getservletfilters () </span> <span style="font-size: 18px;">wil L automatically is mapped to Dispatcherservlet</span></p></p><p><p>SPRING in ACTION 4th edition notes-seventh Chapter advanced Spring mvc-001-dispatcherservlet Configuration (servletregistration.dynamic, Webapplicationinitializer)</p></p></span>
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