SpringFramework_3006

Last modified by superadmin on 2018-01-12 20:19

Back to Item Set

<p/>
<!-- START TestItem -->
<p/>
<div class="TestItem">
<div class="TestItemQuestion">3006 Question:\

What is a correct way to configure <tt>DispatcherServlet</tt> programmatically instead of using <tt>web.xml</tt> configuration?

</div>

Select 1 correct answer(s)!

<p/>
<ol class="MTestItemChoices" type='A'>
<li>

It is not possible

</li>
<li>

Adding the following class:
{code}
@ WebApplicationInitializer
public class SpringWebAppInitializer {
 @Override
 public void onStartup(ServletContext container) {
  XmlWebApplicationContext appContext = new XmlWebApplicationContext();
  appContext.setConfigLocation("/WEB-INF/spring-servlet.xml");
  ServletRegistration.Dynamic dispatcher =
    container.addServlet("dispatcher", new DispatcherServlet(appContext));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("/spring/*");
    }
}
{code}

</li>
<li>

Adding the following class:
{code}
public class SpringWebAppInitializer implements WebApplicationInitializer {
 @Override
 public void onStartup(ServletContext container) {
  XmlWebApplicationContext appContext = new XmlWebApplicationContext();
  appContext.setConfigLocation("/WEB-INF/spring-servlet.xml");
  ServletRegistration.Dynamic dispatcher =
    container.addServlet("dispatcher", new DispatcherServlet(appContext));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("/spring/*");
    }
}
{code}

</li>
<li>

Adding the following class:
{code}
public class SpringWebAppInitializer extends AbstractDispatcherServletInitializer {
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        return new XmlWebApplicationContext/WEB-INF/spring-servlet.xml");
    }
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/spring/*" };
    }
}
{code}

</li>
</ol>
<p/>
<div class="TestItemSkill"><!--$skillid>Skill: $skilltitle</div>
<p/>
</div>
<!-- END MTestItem -->
<p/>
<p/>


{{/velocity}}

Tags:
Created by Alina Vasiljeva on 2013-01-13 11:13
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation