1. Create a Web project from a maven template
You can create a project that launches a Java Web application quickly by using Maven's Maven-archetype-webapp template. In the terminal (* Unix or Mac) or command prompt (Windows), navigate to the folder where you want to create the project.
Type the following command:
$ MVN archetype:generate-dgroupid=com.yiibai-dartifactid=counterwebapp-darchetypeartifactid= Maven-archetype-webapp-dinteractivemode=false
Specific examples:
C:\WORKSP>MVN Archetype:generate-dgroupid=com.yiibai -dartifactid=counterwebapp-darchetypeartifactid= Maven-archetype-webapp-dinteractivemode=false\
The new Web project is named "Counterwebapp", and some standard web directory structures are also created automatically.
2. Project directory Layout
To view the resulting project structure layout:
.| ____counterwebapp| | ____pom.xml| | ____src| | | ____main| | | | ____resources| | | | ____webapp| | | | | ____index.jsp| | | | | ____web-inf| | | | | | ____web.xml
Maven produces some folders, a deployment descriptor Web.xml,pom.xml and index.jsp.
3. Eclipse IDE Support
To import this project into eclipse, you need to generate some configuration files for the Eclipse project:
3.1. In the terminal, go to the "Counterwebapp" folder and type the following command:
C:\WORKSP>CD Counterwebappc:\worksp\counterwebapp>mvn eclipse:eclipse -dwtpversion=2.0[info] scanning for projects ... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/ maven-war-plugin-2.2.pomdownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- war-plugin/2.2/maven-war-plugin-2.2.pom (7 kb at 2.5 kb/sec) Downloading: https:/ /repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.jardownloaded : https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.2/ maven-war-plugin-2.2.jar (77 kb at 26.2 kb/sec) [info][info] -------------------- ----------------------------------------------------[info] building counterwebapp maven webapp 1.0-snapshot[info] ------------------------------------------------------------------------[Info][info] >>> maven-eclipse-plugin:2.10:eclipse (DEFAULT-CLI) > generate-resources@ counterwebapp >>>[info][info] <<< maven-eclipse-plugin:2.10:eclipse (DEFAULT-CLI) < generate-resources@ counterwebapp <<<[INFO][INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ counterwebapp ---[info] adding support for wtp version 2.0. [info] using eclipse workspace: null[info] adding default classpath container: org.eclipse.jdt.launching.jre_container[info] not writing settings - defaults suffice[info] wrote eclipse project for "COUNTERWEBAPP" to C : \worksp\counterwebapp. [INFO] [info] ------------------------------------------------------------------------[info] build success[info] ------------------------------------------------------------------------[Info] total time: 7.982 s[info] Finished at: 2015-10-28T20:24:57+08:00[INFO] Final Memory: 15M/146M[INFO] ------------------------------------------------------------------------
Note thatThis option-dwtpversion=2.0 tells Maven to convert the project to Eclipse's Web project (WAR) instead of the default Java project (JAR). For your convenience, we'll show you how to configure this WTP option in Pom.xml in the future.
3.2 Import to Eclipse ide–file, import ...-Existing Projects into workspace.
Image description: In Eclipse, if you see a globe icon at the top of the project, it means that this is a WEB project.
4. Updating the POM
In Maven, the settings for Web projects are configured through this single pom.xml file.
- Add Project Dependencies-Spring, Logback, and JUnit
- Add plug-ins to configure items
Read the notes clearly and concisely.
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.0 /http Maven.apache.org/maven-v4_0_0.xsd "><modelversion>4.0.0</modelversion><groupid>com.yiibai </groupId><artifactId>CounterWebApp</artifactId><packaging>war</packaging>< Version>1.0-snapshot</version><name>counterwebapp 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></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></dependencies><build><finalname>counterwebapp</finalname ><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 --><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>/counterwebapp</path> </configuration> </plugin></plugins></build></project>
Note that for the sake of 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 into an Eclipse Web project.
#之前 mvn Eclipse:eclipse--Eclipse Java Project (JAR) mvn eclipse:eclipse-dwtpversion=2.0--Eclipse Java Web proj ECT (WAR) #之后mvn Eclipse:eclipse---Eclipse Java Web Project (WAR)
5. Update the source code
In this step, after configuring Pom.xml in the previous step, re-executing mvn eclipse:eclipse This command, we will create some spring MVC files and folders for the Logback log framework, the final project structure is as follows:
.| ____pom.xml|____src| |____main| | |____java| | | |____com| | | | |____yiibai| | | | | |____controller| | | | | | | |____Base controller.java| | |____resources| | | |____logback.xml| | |____webapp| | | |____web-inf| | | | |____mvc-dispatcher-servle t.xml| | | | |____pages| | | | | |____index.jsp| | | | |____web.xml
Note that if it does not exist, you need to create the folder manually.
5.1 Create the Controller class for Spring MVC.
/src/main/java/com/yiibai/controller/basecontroller.java
Package Com.yiibai.controller;import Org.slf4j.loggerfactory;import Org.springframework.stereotype.Controller; Import Org.springframework.ui.modelmap;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controllerpublic class Basecontroller {private static int Counter = 0;private static final String View_index = "INDEX";p rivate final static Org.slf4j.Logger Logger = Loggerfactory. GetLogger (Basecontroller.class); @RequestMapping (value = "/", method = requestmethod.get) Public String Welcome ( Modelmap model) {Model.addattribute ("message", "Welcome"), Model.addattribute ("counter", ++counter); Logger.debug ("[ Welcome] Counter: {} ", counter);//Spring uses Internalresourceviewresolver and return back Index.jspreturn View_index;} @RequestMapping (value = "/{name}", method = requestmethod.get) public String welcomename (@PathVariable string name, Modelmap model) {Model.addaTtribute ("message", "Welcome" + name), Model.addattribute ("counter", ++counter); Logger.debug ("[Welcomename] Counter: {} ", counter); return view_index;}}
5.2 Create a spring configuration file.
/src/main/webapp/web-inf/mvc-dispatcher-servlet.xml
<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 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=" Com.yiibai.controller " />< beanclass= "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 allows the existing Web. XML to support Servlet 2.5 (the default Servlet2.3 is too old) and also integrates the spring framework with 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 the file index.jsp to the Web-inf/pages directory, in order to protect direct access. and update the content:
/src/main/webapp/web-inf/pages/index.jsp
5.5 Create a Logback.xml file in Resource folders (resources)
/src/main/resources/logback.xml
6. Eclipse + Tomcat
After creating all the files in step 5th, here are some ways to deploy and test the Web project, and we recommend using the method in 6.2.
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/counterwebapp.war, simply copied and deployed to the Tomcat published directory.
6.2 If you want to debug through the Eclipse server this project plug-in (Tomcat or other container), then enter:
MVN Eclipse:eclipse
If all goes well, the dependencies of the project will be assembled to the Web deployment project. Picture: Right click on Project --Properties--Deployment Assembly
6.3 Maven's Tomcat plugin declaration (added to Pom.xml):
Pom.xml
Type the following command (sometimes 2-3 times if the network is not unblocked):
MVN tomcat:runtp://logback.qos.ch/codes.html#layoutinsteadofencoder for details20:37:32,089 |- info in ch.qos.logback.classic.joran.action.loggeraction - seg level of logger [com.yiibai.controller] to debug20:37:32,089 |-info in ch.qos.logback.classic.joran.action.loggeraction - seg additivity of logger [ com.yiibai.controller] to false20:37:32,090 |-info in Ch.qos.logback.core.joran.action.appenderrefaction -ching appender named [stdout] to Logger[com.yiibai.controller]20:37:32,090 |-INFO in Ch.qos.logback.classic.joran.action.rootloggeractiontting level of root logger to error20:37:32,090 |-info in ch.qos.logback.core.joran.action.appenderrefaction - ching appender named [stdout] to logger[root]20:37:32,090 |-info in Ch.qOs.logback.classic.joran.action.configurationacti end of configuration.20:37:32,091 |-info in [email protected]Registering current configuration as safe fallback point October 28, 2015 20:37:32 pm Org.apache.catalina.core.ApplicationConte XT Log Info: Initializing Spring root Webapplicationcontext October 28, 2015 20:37:33 pm Org.apache.catalina.core.ApplicationContext Log Info: Initializing Spring frameworkservlet ' Mvc-dispatcher ' October 28, 2015 20:37:33 pm Org.apache.coyote.http11.Http11Protocol init info: Initializing Coyote http/1.1 on http-8080 October 28, 2015 20:37:33 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.
Error: Update maven under MAVEN project after eclipse error: Java.lang.ClassNotFoundException:ContextLoaderL
Solution:
1. Right click on the project-select Properties
Select Deployment Assembly, click the Add button on the right, and select Java Build Path in the pop-up window Entries
2. Click Nextto select Maven Dependencies
3. Click Finish, and you can see that maven dependencies has been added to the Web application structure.
After the operation, redeploy the project, no longer error. Then we go to the. Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ directory, found in the project Web-inf directory automatically generated Lib directory, And all the dependent jar packages have been deployed. The problem is therefore resolved.
maven--Learning (5)--Create a Web project