Create a spring MVC Web project using maven

Source: Internet
Author: User

Using MAVEN to create a Java Web project (Spring MVC) uses the following tools:

1. Maven 3.2

2, IntelliJ idea 13

3. JDK 1.7

4. Spring 4.1.1 Released

5, TOMCAT7

6. Logback 1.0.13 Log Output component

The next step is to show how to create a Web project with a maven template

1. Use Maven-archetype-webapp template to quickly create skeleton structure of Web project

Open the console, go to the directory where you want to create the Web project, and then run the following command:

1 $ mvn archetype:generate-dgroupid=com.yanyd 2     -dartifactid=counterwebapp 3     -darchetypeartifactid= Maven-archetype-webapp 4     -dinteractivemode=false

A project named Counterwebapp is created, and the standard web directory structure is automatically created with the following structure:

1 |____COUNTERWEBAPP2 | |____pom.xml3 | |____src4 | | |____main5 | | | |____resources6 | | | |____webapp7 | | | | |____ind EX.JSP8 | | | | |____web-inf9 | | | | | | |____web.xml

The source package java/com/yanyd is not created automatically, it needs to be created manually, and then it becomes the SRC directory in idea.

2. Use the command mvn Idea:idea to turn the project into an idea-supported project and then program it into the idea.

Next, configure the project-dependent files and compile and deploy the plug-ins in the Pom.xml file, as follows:

  1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 <groupId>com.yanyd</groupId> 5 <artifactId> Counterwebapp</artifactid> 6 <packaging>war</packaging> 7 <version>1.0-snapshot</ Version> 8 <name>counterwebapp Maven webapp</name> 9 <url>http://maven.apache.org</url> 1 0 <properties> <jdk.version>1.7</jdk.version> <spring.version>4.1.1.release&lt ;/spring.version> <jstl.version>1.2</jstl.version> <junit.version>4.11</junit.ve Rsion> <logback.version>1.0.13</logback.version> <jcl-over-slf4j.version>1.7.5</ Jcl-over-slf4j.version> </properties> <d ependencies> <dependency> <groupId>junit</groupId> <artifactid>junit     </artifactId> <version>${junit.version}</version> 24 <scope>test</scope> </dependency> <!--spring core--> <dependency> &LT;GROUPID&GT;ORG.SPR Ingframework</groupid> <artifactId>spring-core</artifactId> <version>${s pring.version}</version> <exclusions> <exclusion> &lt               ;groupid>commons-logging</groupid> <artifactId>commons-logging</artifactId> 35 </exclusion> </exclusions> Notoginseng </dependency> <dependency& Gt           <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> 42 <versioN>${jcl-over-slf4j.version}</version> </dependency> <dependency> 46 &           Lt;groupid>ch.qos.logback</groupid> <artifactId>logback-classic</artifactId> 48           <version>${logback.version}</version> </dependency> 52 <dependency>           <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> 54           <version>${spring.version}</version> </dependency> 58  <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> Jstl <!----&G T <dependency> <groupId>jstl</groupId> <artifactid>jstl</artif         Actid> 67  <version>${jstl.version}</version> </dependency> <buil </dependencies> D> <!--The resulting war file name--all <finalName>CounterWebApp</finalName> 75 <plugins> 7 6 <!--Set JDK Compiler level-to-all <plugin> &LT;GROUPID&GT;ORG.APACHE.MAVEN.P Lugins</groupid> <artifactId>maven-compiler-plugin</artifactId> <versio N>3.2</version> Bayi <configuration> &LT;SOURCE&GT;${JDK.VERSION}&LT;/SOURCE&G T <target>${jdk.version}</target> </configuration> </plugi N> <!--for Maven Tomcat Plugin---<plugin> <groupid>org.ap             Ache.tomcat.maven</groupid> <artifactId>tomcat7-maven-plugin</artifactId> 91 <version>2.2</version> <configuration> <!--Deploy to server--> 94 &lt                 ;url>http://localhost:8080/manager/text</url> <server>Tomcat7Server</server> 96     <path>/CounterWebApp</path> </configuration> 98 </plugin> 99 </plugins>100 </build>101 </project>

3, write the source file, test the Spring MVC Framework is normal, the final file structure is as follows:

1 |____pom.xml 2 |____src 3 | |____main 4 | | |____java 5 | | | |____com 6 | | | | |____yanyd 7 | | | | | |____controller 8 | | | | | | |____basecontroller.java 9 | | |____resources10 | | | |____logback.xml11 | | |____webapp12 | | | | |____web-in F13 | | | | | |____mvc-dispatcher-servlet.xml14 | | | | |____pages15 | | | | | |____index.jsp16 | | | | | |____web.xml

Start writing the Spring MVC control class, Basecontroller.java:

 1 package Com.yanyd.controller; 2 3 Import Org.slf4j.Logger; 4 Import org.slf4j.LoggerFactory; 5 Import Org.springframework.stereotype.Controller; 6 Import Org.springframework.ui.ModelMap; 7 Import org.springframework.web.bind.annotation.PathVariable; 8 Import org.springframework.web.bind.annotation.RequestMapping;  9 Import org.springframework.web.bind.annotation.requestmethod;10/**12 * Created by Administrator on 15-1-31.13 */14 /*there must be a Controller annotation or the application would doesn ' t work. */15 @Controller16 public class Basecontrol  ler {counter=0;18-private static int-private static final String view_index= "INDEX"; Logger logger= Loggerfactory.getlogger (basecontroller.class); @RequestMapping (value = "/", method = Requestmetho D.get) of public String Welcome (Modelmap model) {Model.addattribute ("message", "Welcome"); Model.add Attribute ("Counter", ++counter); Logger.debug ("[Welcome counteR: {} ", counter); return view_index;//returns INDEX.JSP27}28 @RequestMapping (value ="/{name} ", method = Re Questmethod.get) The public string welcome (@PathVariable string name, Modelmap model) {Model.addattribute ("Me Ssage "," Welcome "+name), Model.addattribute (" counter ", ++counter), Logger.debug (" [Welcome counter: {} ", counter); return view_index;//returns INDEX.JSP35}36}

One thing to note is that the controller annotations must be written, otherwise after the deployment of access will appear 404, has been unable to find the problem where!

Then write the spring configuration file, Mvc-dispatcher-servlet.xml:

 1 <beans xmlns= "Http://www.springframework.org/schema/beans" 2 xmlns:context= "HTTP://WWW.SPRINGFRAMEWORK.O         Rg/schema/context "3 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "4 xsi:schemalocation=" 5         Http://www.springframework.org/schema/beans 6 Http://www.springframework.org/schema/beans/spring-beans.xsd 7 Http://www.springframework.org/schema/context 8 Http://www.springframework.org/schema/context/spring-context.xs D "> 9 <context:component-scan base-package=" Com.yanyd.controller "/>11 <bean class=" Org.springframew Ork.web.servlet.view.InternalResourceViewResolver ">12 <property name=" prefix ">13 <value> ;/web-inf/pages/</value>14 </property>15 <property name= "suffix" >16 <valu e>.jsp</value>17 </property>18 </bean>19 </beans> 

Finally, the spring framework is integrated into the Web. xml file and the listener is configured:

 1 <web-app xmlns= "Http://java.sun.com/xml/ns/javaee" 2 xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee 4 http://java.sun.com/xml/ns/javaee/web-app_2_5 . xsd "5 version=" 2.5 "> 6 7 <display-name>counter WEB application </display-name> 8 9 & Lt;servlet>10 <servlet-name>mvc-dispatcher</servlet-name>11 <servlet-class>org.spring Framework.web.servlet.dispatcherservlet</servlet-class>12 <load-on-startup>1</load-on-startup >13 </servlet>14 <servlet-mapping>15 <servlet-name>mvc-dispatcher</servlet-name&gt         ; <url-pattern>/</url-pattern>17 </servlet-mapping>18 <context-param>20 <param-name>contextconfiglocation</param-name>21 <param-value>/web-inf/mvc-dispatcher-servlet    . xml</param-value>22 </context-param>23 <listener>25 <listener-class>org.springframework.web.context.cont Extloaderlistener</listener-class>26 </listener>27 </web-app>

In order to avoid direct access to our JSP files, we need to create a pages directory under the Web-inf directory and put the JSP file inside, index.jsp content as follows:

1 

In order for the log output component to work, you will also need to create a log output configuration file in the Resources directory Logback.xml:

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <configuration> 3  4     <appender name= "STDOUT" class= " Ch.qos.logback.core.ConsoleAppender "> 5         <layout class=" Ch.qos.logback.classic.PatternLayout "> 6  7             <Pattern> 8                 %d{yyyy-mm-dd HH:mm:ss} [%thread]%-5level%logger{36}-%msg%n 9             </pattern>10 11         </layout>12     </appender>13     <logger name= "Com.yanyd.controller" level= "Debug" 15             additivity= "false" >16         <appender-ref ref= "STDOUT"/>17     </logger>18     Level= "Error" >20         <appender-ref ref= "STDOUT"/>21     </root>22  >

4. Deploying Web Projects

Method 1:MVN The package and deploy the resulting war file to Tomcat.

Method 2:mvn Idea:idea, the project is transferred to the project that supports idea and is deployed directly in idea.

Method 3:mvn Tomcat:run, if the Tomcat deployment plugin is already configured in the Pom file, running the command directly will deploy the project to port 8080.

5. Access to the project

http://localhost:8080/CounterWebApp/

Http://localhost:8080/CounterWebApp/yanyd

If the content is output properly, then the deployment is successful and the project structure is no problem!

Create a spring MVC Web project using maven

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.