Build SPRINGMVC framework based on MAVEN from scratch

Source: Internet
Author: User

0. Preparatory work

First, prepare the eclipse and required plugins, such as the Maven plugin, the Spring IDE plugin.

1. Build a WebApp project under Maven

1. Create a new MAVEN project with type WebApp, such as

2. Then name the project, add GroupID, etc.

3. Configure the project's publishing directory, under Deployment assemly,

2. Configure Spring and Maven

1. Configure Pom.xml to add the following package dependencies. The version does not have to correspond, may use some new package behind, missing which package can follow up to Baidu then joins in Pom.xml

<Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>3.8.1</version>            <Scope>Test</Scope>        </Dependency>        <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-web</Artifactid>            <version>4.1.2.RELEASE</version>        </Dependency>        <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-webmvc</Artifactid>            <version>4.1.2.RELEASE</version>        </Dependency>        <Dependency>            <groupId>Javax.servlet</groupId>            <Artifactid>Javax.servlet-api</Artifactid>            <version>3.1.0</version>        </Dependency>

2. Configure the Web. xml file, add the Contextloaderlistener listener, assemble the configuration profile configuration when the servlet starts, and then add Springdispatcherservlet, specify the MVC configuration file, and configure the MVC framework. All configurations are as follows:

<!DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_ 3.dtd "><Web-app>  <Display-name>Archetype Created Web Application</Display-name>  <!--needed for Contextloaderlistener -    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>/web-inf/spring-context.xml,/web-inf/spring-hibernate.xml</Param-value>    </Context-param>    <!--bootstraps The root Web application context before servlet initialization -    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <!--the front controller of this Spring WEB application, responsible-handling all application requests -    <servlet>        <Servlet-name>Springdispatcherservlet</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>/web-inf/spring-mvc.xml</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <!--set the listening position, '/' for all listening -    <servlet-mapping>        <Servlet-name>Springdispatcherservlet</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping></Web-app>

3. Configure Spring-mvc.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "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.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " >    <!--activating @controller Mode -    <Mvc:annotation-driven/>    <!--Configure the Package scan location (the @controller controller will be scanned under this package) -    <Context:component-scanBase-package= "Com.test.maven.controller" />    <!--Configuring the View resolver (JSP file prefix suffix) -    <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix">            <value>/web-inf/jsp/</value>        </ Property>        < Propertyname= "suffix">            <value>. jsp</value>        </ Property>    </Bean>    <!--Configure the tiles template (no tiles can be configured without this item) -    <BeanID= "Tilesconfigurer"class= "Org.springframework.web.servlet.view.tiles3.TilesConfigurer">        < Propertyname= "Definitions">            <List>                <value>/web-inf/tiles/tiles-definitions.xml</value>            </List>        </ Property>    </Bean></Beans>

4. Configure Spring-context.xml. The simplest frame is built for the time being, so there is no need to configure it temporarily.

The most basic SPRINGMVC framework is built. By loading the project into Tomcat and running the main directory that accesses the project, the default index.jsp is called and the Hello World is displayed.

Build SPRINGMVC framework based on MAVEN from scratch

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.