First of all, Eclipse needs to install Maven's plug-in, address: http://m2eclipse.sonatype.org/sites/m2e.
Using MyEclipse to install the Maven plug-in, there are some problems with the Maven project you created. First, resources will never be published under tomcat when publishing tomcat; second, change classes under WEB-INF to classes under target, but I don’t know why MyEclipse either still generates classes under WEB-INF. Either I really don't generate classes in WEB-INF, but when I publish tomcat, I won't publish any class files to you, which is super depressing. But after using Eclipse to build a Maven project, it will be fine to open it with MyEclipse.
The benefits of using maven: I feel that the most important thing is to automatically download the jar package and the package it depends on, which can ensure the problem of different jar versions when multiple people develop. In addition, the file structure is clear, and the Java files, resource files, and test files are all clearly divided.
Two methods will be introduced: one, directly establish the Maven project method; two, establish the Dynamic Web project method to convert it into the Maven project method.
1. Directly establish the Maven project method
1. Establish a Maven project
Next, use Eclipse's maven to build a web project, taking the SpringMVC project as an example:
1.1 Select Create Maven Project
Select File -> New -> Other, and select Maven -> Maven Project in the New window. Click next.
1.2 Select the project path
Use default Workspace location default workspace.
1.3 Select the project type
Select maven-archetype-webapp in Artifact Id
1.4 Enter Group ID and Artifact ID, as well as Package
Group ID is generally written with the name of the large project. The Artifact ID is the subproject name.
For example, Spring's web package, Group ID: org.springframework, artifactId: spring-web.
Package is the default package for you, and it is OK to not write it.
1.5 The newly established file structure is as follows
If there is a lot of content displayed here, it is generally a problem with Filters settings. Or perspective is JavaEE mode, just change it to Java mode.
2. Configure Maven project
Next, many configurations need to be changed.
2.1 Add Source folder
Next, you need to add three folders: src/main/java, src/test/java, and src/test/resources. Right-click the project root directory and click New -> Source Folder.
Create these three folders. Note that it is not a normal Folder, but a Source Folder.
2.2 Change the class path
Right-click the project, Java Build Path -> Source
There should be 4 folders below. src/main/java, src/main/resources, src/test/java, src/test/resources.
Double-click the Output folder for each folder and select the path.
src/main/java, src/main/resources, select target/classes;
src/test/java , src/test/resources, select target/test-classes;
Select Allow output folders for source folders.
Also change here:
Change the order in which folders are displayed: Click Order and Export.
Change the JDK version: Double-click the JRE System Library in Libraries to version 1.6.
2.3 Turn a project into a Dynamic Web project
2.3.1 Right-click the project, select Project Facets, and click Convert to faceted from
2.3.2 Configuring Project Facets
Change the Version of Dynamic Web Module to 2.5. (3.0 is Java7).
If an error is prompted, you may need to set the Compiler compliance level to 1.6 in the Java Compiler. Or you need to change the Java Version of this window to 1.6.
2.3.3 Configuring Modify Faceted Project
Click the Further configuration available… to pop up the Modify Faceted Project window
Here is the path to set the web.xml file, and we enter src/main/webapp.
Generate web.xml deployment descriptor automatically generates web.xml file, optional or not.
2.4 Setting up the deployment assembly (Web Deployment Assembly)
After the above steps are set up, click OK and the Properties window will close. Right-click the project to open this window. A Deployment Assembly will appear in the list on the left. After clicking in, as shown below:
Here the list is the path to the file release when the project is deployed.
1. We delete the two items of test because test is used for testing and does not require deployment.
2. Set up the Maven jar package to lib.
Add -> Java Build Path Entries -> Maven Dependencies -> Finish
Set the finished rendering
3. Add jar package to the maven project
maven can manage jar packages that the project depends on, and can uniquely determine a jar package through groupID, artifactId and version number. This can prevent the problem of inconsistent jar packages under WEB-INF/lib in old web projects. In addition, maven will automatically download the jar packages that are relied on by the added jar package.
3.1 Add the required jar package in pom.xml
Use the Maven POM editor to open the pom.xml file in the project, select Dependencies, click Add in the Dependencies column, and a search button pops up first, for example, enter spring-web, and it will automatically search for the jar package related to spring-web. We select the 3.0.5 version of spring. Add all spring packages. Other jar packages that need to be added include: junit, jstl. Or click pom.xml to edit the pom.xml file directly. In this way, you can copy the dependencies content directly.
3.2 Set the scope of the jar package
When adding to a jar package, there are some properties that need to be set. The most important thing is scope, which has the following values:
1.compile, default value, applies to all stages and will be released along with the project.
2.provided, similar to compile, expects that JDK, container or user will provide this dependency. Such as servlet.jar.
3.runtime, only used at runtime, such as JDBC driver, suitable for run and test stages.
4.test, used only when testing, is used to compile and run test code. Will not be released with the project.
5.system, similar to provided, requires explicitly providing a jar containing dependencies, and Maven will not look for it in Repository.
Usually, the scope jar package required for SpringMVC projects is as follows:
Sometimes I find that the servlet-api is still packaged under the lib, and an error will definitely be reported at this time. You need to install WTP in the maven plugin as well.
Eclipse online installation path: http://m2eclipse.sonatype.org/sites/m2e-extras. Select for Eclipse WTP.
4. Build SpringMVC framework
4.1 Edit the web.xml file
You need to add log4j, character filtering, Spring dispatcher, etc.
The webx.xml code is as follows:
<?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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" > <!-- Distinguish project names and prevent default duplicate names --> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.example.root</param-value> </context-param> <!-- Spring's log4j listener--> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Character Set Filter--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view distributor--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
4.2 Write Spring configuration file dispatcher-servlet.xml
For example, you want to add MVC driver, annotation detection, view analysis, etc. The dispatcher-servlet.xml code is as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="liming.maven.example" /> <bean> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
4.3 Write a Controller layer test class
Write a SpringMVC Controller layer test class. There is only one method for address mapping and passes a data to the page. The code is as follows:
package liming.maven.example.view; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class GeneralController { @RequestMapping(value="index.do") public void index_jsp(Model model){ model.addAttribute("liming", "Hello Dawn"); System.out.println("index.jsp"); } }4.4 Write index.jsp page
First, create folder views under src/main/webapp/WEB-INF. Here is the path of the prefix attribute in the dispatcher-servlet.xml configuration file.
Create index.jsp file under views
We use jstl to get the data of Controlleradd.
The Jsp page code is as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <c:out value="${liming}"></c:out> </body> </html>5. Publish to tomcat
There is nothing to say about this.
6. Test
Access address: http://localhost:8080/liming.maven.example/index.do
Here is a screenshot of the visit result:
2. Create a Dynamic Web project to convert it into a Maven project
7. The second method is to convert DynamicWeb project to Mavan
7.1 Create a new Dynamic Web Project
Select File -> New -> Other. In the New window, select Dynamic Web Project under the Web. Click next.
7.2 Configure project information
7.2.1 Enter project information
First enter the project name.
Target runtime generally chooses what container to use, tomcat and so on. You can not choose first.
Dynamic web module version. That is the version number. Choose 2.5 (3.0 is Java7's).
Configuration is an easy configuration to choose. If Target runtime selects tomcat. This corresponds to the choice of tomcat.
Next.
7.2.2 Configure the folder of source and output
This window enters the Source folders you want to create. and default output folder.
We add this after building the project.
The output folder can be changed to target/classes.
7.2.3 Configure the path to the web.xml file
7.3 Convert to maven project
After the Dynamic web project is established, first convert it into a maven project.
Right-click this item and select maven -> Enable Dependency Management. The pop-up window is just finished.
7.4 Other configurations
The following configuration work is basically the same as before.
Please refer to the above.
2.1 Add Source folder
2.2 Change the class path.
2.3.3 Configuring Modify Faceted Project
2.4 Setting up the deployment assembly (Web Deployment Assembly)
Next is to build the SpringMVC framework, publish it to tomcat, and test it.
Attachment download:
liming.maven.example.zip
liming.dynamic.example.zip