1. Create a project
MVN archetype:generate-dgroupid=cn.dinfo.boc.bocsearch-dartifactid=bocsearch-darchetypeartifactid= Maven-archetype-webapp-dinteractivemode=false
2. Directory structure of the project:
.| ____bocsearch
| | ____pom.xml
| | ____src | | |
____main | | | |
____resources | | | |
____webapp | | | |
____index.jsp | | | |
____web-inf | | |
____web.xml
3. Modify the configuration file:
1. Modify Pom.xml:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dinfo.boc.bocsearch</groupId>
<artifactId>bocsearch</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>bocsearch Maven webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<spring.version>4.1.1.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
<logback.version>1.0.13</logback.version>
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--solve packed Chinese garbled-->
</properties>
<dependencies>
<!--unit Test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--Spring Core;
<dependency>
<groupId> Org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupid>commons-logging</ Groupid>
<artifactid>commons-logging</artifactid>
</exclusion>
</exclusions>
</dependency >
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over-slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!--Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>BocWebApp</finalName><!--Set the name of the war package-->
<plugins>
<!--Eclipse Project
<PLUGIN>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-eclipse-plugin</artifactid>
<version>2.9 </version>
<configuration>
<!--Always download and attach dependencies source code;
< Downloadsources>true</downloadsources>
<downloadjavadocs>false</ Downloadjavadocs>
<!--Avoid type mvn eclipse:eclipse-dwtpversion=2.0;
& Nbsp; <wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<!--Set JDK Compiler level;
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactId> Maven-compiler-plugin</artifactid>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration
</plugin>
<!--for Maven Tomcat Plugin-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/CounterWebApp2</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. Modify Web.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
version= "2.5" >
<display-name>boc Web application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
Org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
3. Eclipse IDE Support: Enter Bocsearch directory to execute the following command:
MVN eclipse:eclipse-dwtpversion=2.0 Note that this option-dwtpversion=2.0 tells Maven to convert the project to Eclipse's Web project (WAR) instead of the default Java project (JAR). For convenience, later we will show you how to configure this WTP option in Pom.xml.
3.2 Import to Eclipse ide–file-> import ...-> general-> Existing to Projects. Import a project into eclipse
4. Modify POM configuration file:
<plugins>
<!--Eclipse project-->
<plugin>
<groupid>org.apache.maven.plugins </groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</ version>
<configuration>
<!--Always download and attach dependencies source code-->
& Lt;downloadsources>true</downloadsources>
<downloadJavadocs>false</downloadJavadocs>
<!--avoid type mvn eclipse:eclipse-dwtpversion=2.0-->
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
Note that for convenience, declare maven-eclipse-plugin and configure Wtpversion to avoid input parameter-dwtpversion=2.0. Now, each time you use MVN Eclipse:eclipse,maven to convert this project import into an Eclipse Web project.
#之前
mvn eclipse:eclipse--> Eclipse Java Project (JAR)
mvn eclipse:eclipse-dwtpversion=2.0--> Eclipse Jav A Web project (war)
#之后
mvn eclipse:eclipse--> Eclipse Java Web Project (WAR)
5. Update code:
In this step, after you have configured the Pom.xml in the previous step, mvn eclipse:eclipse This command, we will create some of the files and Logback Log framework folders for Spring MVC.
Note that if it does not exist, you will need to create the folder manually.
/src/main/java/cn.dinfo.boc.bocsearch.controller/logincontroller.java
@Controller
public class Logincontroller {
private static int counter = 0;
private static final String View_index = "result";
Private final static Org.slf4j.Logger Logger = Loggerfactory.getlogger (Logincontroller.class);
@RequestMapping (value = "/login", method = Requestmethod.get)
Public String Welcome (Modelmap model) {
Model.addattribute ("message", "Welcome ljf");
Model.addattribute ("Counter", ++counter);
Logger.debug ("[Welcome] counter: {}", counter);
Spring uses Internalresourceviewresolver and return back index.jsp
return view_index;
}
}
5.2 Create the spring configuration file. /src/main/webapp/web-inf/mvc-dispatcher-servlet.xml
<?xml version= "1.0" encoding= "UTF-8"
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:xsi= http://www.w3.org/ 2001/xmlschema-instance "
xsi:schemalocation="
http:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS&NBSP;&NBSP;&NBSP;&NBSP
http ://www.springframework.org/schema/beans/spring-beans.xsd http:/ /www.springframework.org/schema/context
http:// Www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package= "Cn.dinfo.boc.bocsearch.controller"/>
<bean
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" >
<value>/WEB-INF/pages/</value>
</property>
<property name= "suffix" >
<value>.jsp</value>
</property>
</bean>
</beans>
The 5.3 Update enables the existing Web.xml to support Servlet 2.5 (the default Servlet2.3 is too old), and the spring framework is also integrated through the spring listener Contextloaderlistener. /src/main/webapp/web-inf/web.xml
<web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/ javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "> <display-name>Counter Web Application</display-name> <servlet> <servlet-name >mvc-dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> < /servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</
Param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener
</listener-class> </listener> </web-app>
5.4 Move files result.jsp to web-inf/pages directory, in order to protect direct access. and update the content:
5.5 Start the error by creating the Logback.xml file in the Resource folder (resources) (copy the following statement to an empty XML file named Logback.xml), and then delete the file/src/main/resources/ Logback.xml
6. Pack and run
1.
6.1 To compile, test and project package into a war file, enter:
MVN Package
A new WAR file will be generated in Project/target/bocwebapp.war, just copy and deploy to the directory that Tomcat publishes.
2.
If you want to debug through the Eclipse server, the project plug-in (Tomcat or other container), enter:
MVN Eclipse:eclipse
If all goes well, the dependencies of the project will be assembled to be attached to the Web deployment project. Photo: Right-click Project-> Properties-> Deployment Assembly
6.3 Maven's Tomcat Plug-in declaration (add to Pom.xml): Pom.xml
<!--for Maven Tomcat Plugin-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/CounterWebApp</path>
</configuration>
</plugin>
Enter command:
MVN Tomcat:run
13:59:18.538 [main] DEBUG o.s.c.e.propertysourcespropertyresolver-searching for key ' Spring.liveBeansView.mbeanDomain ' in [systemenvironment]
13:59:18.541 [main] DEBUG o.s.c.e.propertysourcespropertyresolver-could not find key ' Spring.liveBeansView.mbeanDomain ' in any property source.
13:59:18.543 [main] DEBUG o.s.web.servlet.dispatcherservlet-published webapplicationcontext of Servlet ' Mvc-dispatcher ' as ServletContext attrib
13:59:18.545 [main] INFO o.s.web.servlet.dispatcherservlet-frameworkservlet ' mvc-dispatcher ': initialization Completed in 797 MS
13:59:18.548 [main] DEBUG o.s.web.servlet.dispatcherservlet-servlet ' Mvc-dispatcher ' configured successfully
March 11, 2016 1:59:18 pm Org.apache.coyote.http11.Http11Protocol Init
Info:initializing Coyote http/1.1 on http-8080
March 11, 2016 1:59:18 pm Org.apache.coyote.http11.Http11Protocol start
Info:starting Coyote http/1.1 on http-8080
This will start Tomcat, and the deployment project defaults to port 8080.
Details Reference URL: http://www.yiibai.com/maven/maven_environment_setup.html