Using MAVEN to build SPRINGMVC

Source: Internet
Author: User

1. Create a new MAVEN project, select WebApp, click Next, enter GroupID and Artifactid (i.e. project name) and click Finish.

2, at this time the project will error, as follows:

By prompting you to know that you cannot find the HttpServlet class, you can either import tomcat into your working directory or add the Servlet-api.jar of the HttpServlet class through Maven.

1 <Dependency>2 <groupId>Javax.servlet</groupId>3 <Artifactid>Javax.servlet-api</Artifactid>4 <version>3.0.1</version>5 <Scope>Compile</Scope>6 </Dependency>

3. Now add the SPRINGMVC required jar package through MAVEN, click the Add button in Pom.xml, and enter SPRING-WEBMVC in the middle input box:

1 <Dependency>2       <groupId>Org.springframework</groupId>3       <Artifactid>Spring-web</Artifactid>4       <version>4.1.2.RELEASE</version>5       <Scope>Compile</Scope>6     </Dependency>7     <Dependency>8       <groupId>Org.springframework</groupId>9       <Artifactid>Spring-webmvc</Artifactid>Ten       <version>4.1.2.RELEASE</version> One       <Scope>Compile</Scope> A     </Dependency>
View Code

4. Modify Web. xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "3.0"xmlns= "Http://"3 Xmlns:xsi= "Http://"4 xsi:schemalocation= "Http://">5 6     <servlet>7         <Servlet-name>Spring</Servlet-name>8         <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>9     </servlet>Ten  One     <servlet-mapping> A         <Servlet-name>Spring</Servlet-name> -         <Url-pattern>/</Url-pattern> -     </servlet-mapping> the  - </Web-app>

<servlet-name> properties are arbitrary, as long as the upper and lower consistent, Url-pattern "/" to intercept all requests.

5, the above configuration will automatically go to Web-inf under the search for ' servlet-name '-servlet.xml (here corresponds to spring-servlet.xml), the specific content is as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://"3 Xmlns:xsi= "Http://"Xmlns:context= "Http://"4 Xmlns:tx= "Http://"Xmlns:mvc= "Http://"5 xsi:schemalocation= "Http:// Http:// Http:// One Http:// A "> -  -     <!--Configuring a scanned package - the     <Context:component-scanBase-package= "com.springdemo.*" /> -  -     <!--Register Handlermapper, handleradapter two mapping classes - -     <Mvc:annotation-driven/> +  -     <!--Accessing static resources - +     <Mvc:default-servlet-handler/> A      at     <!--View Resolver - -     <Bean -         class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> -         < Propertyname= "prefix"value= "/web-inf/view/"></ Property> -         < Propertyname= "suffix"value= ". jsp"></ Property> -     </Bean> in      - </Beans>

This configuration automatically scans all packages under Com.springdemo for annotated classes (such as @controller, @Service, etc.), <mvc:annotation-driven/> Registers two mapping classes, Responsible for mapping requests to classes and methods, because the configured spring is to intercept all requests, so you need to configure <mvc:default-servlet-handler/> to allow static resources to pass (such as JS, CSS files, etc.) The view parser presents the view name returned by the Controller class plus the configured prefix.

Create a View folder under Web-inf and create a JSP page in it:

<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http:// "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Home</title></Head><Body>    <H1>This is SPRINGMVC Demo</H1></Body></HTML>

Create the Com.springdemo.controller package (the scan path that the package path needs to be configured to scan to), in which you create the controller class, plus annotations:

1  PackageCom.springdemo.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 6 @Controller7@RequestMapping ("/demo")8  Public classDemocontroller {9 Ten@RequestMapping ("/index") One      PublicString Index () { A         return"Demo"; -     } -}

When the request index is mapped to this method, the returned string demo is configured to be web-inf/view/demo.jsp and displayed.

6, in Tomcat deployment run, access to Http://localhost:8080/SpringDemo/index, the following interface is visible, the configuration is successful.

Code See: Https://

Specific use visible Official document:

Using MAVEN to build 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: 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.