This article introduces the detailed steps of Eclipse using maven to build spring mvc for your reference. The specific content is as follows
1. Environment configuration
a). Java 1.7
b). Eclipse luna
c). Maven3.2.5
d).Spring 4.1.4
2. Create a maven project
a). Open eclipse, file->new->project->Maven->Maven Project
b). Next step
c). Select the created project as webapp, next step
d). Fill in the group id and artifact id of the project. Generally speaking, group id writes the reverse order of the domain name, and artifact id writes the project name. Finish the last point.
e). After initially building, the project directory structure is as follows
f). In general project directories, in the java Resources directory, there are three source folders: src/main/java, src/main/test/java, and src/main/test/resources, which need to be created manually. In the following steps, we will talk about how to fill these three directories.
3. Modify the basic project settings
a). Right-click the project name ->Properties ->Java Build path and click the source tag.
b). Prompt hello/src/main/java (missing) and hello/src/test/java (missing). In general project directories, in the java Resources directory, there is also a source folder src/main/test/resources. Delete the missing ones first, then recreate them, and create the missing ones directly. Right-click the key to delete and add.
c). Modify it completely, the effect is as shown in the figure below
d). Next, modify the configuration of libraries, jre uses version 1.7. Select JRE System Library->edit to change the version.
e). Change the configuration in order and export again. The main thing is to adjust the display order of these four directories and adjust the order you like
f). Next, modify project facets and first modify java to 1.7.
Dynamic Web Module cannot be directly modified to 3.0 here. You need to open a .settings folder in the project directory, open org.eclipse.wst.common.project.facet.core.xml, and make the following modifications:
<installed facet="jst.web" version="3.0"/>
Restart eclipe and you will see that the changes take effect.
4. Maven configuration in Eclipse
a).window->properties->maven, check the download repository index updates on startup
5. Simple Spring mvc configuration
a). Open the pom.xml file in the project, click the Dependencies tab, and click add to add to add new dependencies
b). If you know the dependency group id and artifact id, you can fill it in directly. If you are not clear, you can enter keywords to query, or search at http://search.maven.org
c). The dependencies that need to be added are: spring-webmvc, version 4.1.4. RELEASE. The complete POM.XML file content is as follows:
<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.springstudy</groupId> <artifactId>study</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>study Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.1.4.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>study</finalName> </build> </project>d). Open the src/main/webapp/WEB-INF/web.xml file and finally modify it as follows:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="study" version="2.5"> <display-name>Archetype Created Web Application</display-name> <description>sprintMVC environment construction</description> <!-- Loading Spring configuration file--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/configs/spring-*.xml</param-value> </context-param> <!-- Spring listening--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC configuration --> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Customize the configuration file name and path of spring mvc --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:configs/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- spring mvc request suffix --> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
e). Create a configs folder in the Java Resources/scr/main/resources directory to store the configuration path declared in web.xml
f). Create spring-servlet.xml in the Java Resources/scr/main/resources/configs directory, with the following content:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:annotation-config/> <context:component-scan base-package="com.springstudy.controller" /> <mvc:annotation-driven /> <mvc:resources mapping="/styles/**" location="/styles/" /> <mvc:resources mapping="/scripts/**" location="/scripts/" /> <mvc:resources mapping="/images/**" location="/images/" /> <bean > <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
g). Create the controller package. In the spring-servlet.xml file, the path has been specified.
h). In the src/main/webapp/WEB-INF directory, create the views file. In the spring-servlet.xml file, the view file path has been specified
i). Create the first controller file HelloController.java, the complete file content is as follows:
package com.springstudy.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv =new ModelAndView(); mv.addObject("spring", "spring mvc"); mv.setViewName("hello"); return mv; } }j). Add src/main/webapp/WEB-INF/views/hello.jsp file, the content is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>sprint hello</title> </head> <body>hello ${spring}! </body> </html>6. Publish the project to tomcat
a). Add tomcat 7 in eclipse;
b). After the tomcat is added, double-click to set the settings of Server Locations in the overview tab;
i. Select Use Tomcat installation(takes control of Tomcat installation)
ii. Change the content of the Deploy path to: webapps
iii.Save
c). Right-click tomcat, Add and Remove… , add study
d). Start tomcat;
e). Open http://localhost:8080/study/hello in the browser and the access is successful! As shown in the figure below:
The operation is over!
The above is all the content of Eclipse using maven to build spring mvc. I hope it can give you a reference and I hope you can support Wulin.com more.