Replace Web. XML with Webapplicationinitializer configuration Springmvc

Source: Internet
Author: User

For the Dispatcherservlet configuration of Spring MVC, the traditional XML-based approach, the official xml-based, is as follows:

<servlet>   <Servlet-name>Dispatcher</Servlet-name>   <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>   <Init-param>     <Param-name>Contextconfiglocation</Param-name>     <Param-value>/web-inf/spring/dispatcher-config.xml</Param-value>   </Init-param>   <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping>   <Servlet-name>Dispatcher</Servlet-name>   <Url-pattern>/</Url-pattern> </servlet-mapping>

But the spring documents suggest that we adopt code-based this way, of course, the core is to implement Webapplicationinitializer this interface, see the source of this interface, it is very simple, there is only one method, The parameters passed in are ServletContext:

void throws Servletexception;

Here is a small example:

1. Project structure:

2.mywebappinitializer.java

 Public classMywebappinitializerImplementswebapplicationinitializer{ Public voidOnstartup (ServletContext ServletContext)throwsservletexception {xmlwebapplicationcontext appContext=NewXmlwebapplicationcontext (); Appcontext.setconfiglocation ("Classpath:spring-mvc.xml"); Servletregistration.dynamic Dispatcher= Servletcontext.addservlet ("Dispatcher",NewDispatcherservlet (AppContext)); Dispatcher.setloadonstartup (1); Dispatcher.addmapping ("/"); }    }

3.usercontroller.java

@Controller  Public class Usercontroller {        @RequestMapping ("/show")    public  String Show () {         return "Show";    }}

4.spring-mvc.xml

<?XML version= "1.0" encoding= "GBK"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:cache= "Http://www.springframework.org/schema/cache"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"XMLNS:JPA= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"xsi:schemalocation= "Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd/HTTP Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http:/ /www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-3.1.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " >            <Context:component-scanBase-package= "Com.aijava.springcode"/>        <Mvc:annotation-driven/>        <!--Configuring the View resolver -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value="/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean>    </Beans>

5.show.jsp

A simple sentence to print:

< Body >     Show page; </ Body >

6. After you start Tomcat and enter the URL, you will see the result, and the code-based is successful!

Replace Web. XML with Webapplicationinitializer configuration Springmvc

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.