Step-by-step we're trying to use Eclipse J2ee+maven+tomcat (Jetty) to build a retaining web development platform for the Spring MVC approach.
Tools: Eclipse J2ee,maven,tomcat
Eclipse J2ee+maven+tomcat (Jetty): http://www.eclipse.org/downloads/
Maven 3.3.3:http://maven.apache.org/download.cgi
Tomcat 7.x:http://tomcat.apache.org/
1. Install the base environment:
There's nothing to say, Jdk,eclipse,maven, configure environment variables, and so on.
2. Open Eclipse, do web development, we also need to install some plugins.
Eclipse Plugin Address: Luna-http://download.eclipse.org/releases/luna (Install Web,xml,java EE and OSGI Enterprise development)
3. Associate maven with Eclipse. Slightly
4. Create a new MAVEN Web project.
5. Modify the Pom.xml file to write the compile level, the required jar and other information to the Pom.xml file. Then Alt+f5 refreshes the project.
<?xml version= "1.0" encoding= "UTF-8"?><!--Licensed to the Apache software Foundation (ASF) under one or more con Tributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable law or AG Reed to in writing, software distributed under the License are distributed on a "as is" BASIS, without warranties OR Condi tions of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --><!--$Id: Pom.xml 642118 2008-03-28 08:04:16z Reinhard $--><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><packaging>war</packaging><name>demo</name><groupid> com.springmvc</groupid><artifactid>demo</artifactid><version>0.0.1-snapshot</ version><build><plugins><plugin><groupid>org.mortbay.jetty</groupid>< Artifactid>maven-jetty-plugin</artifactid><version>6.1.7</version><configuration> <connectors><connector implementation= "Org.mortbay.jetty.nio.SelectChannelConnector" ><port> 8888</port><maxidletime>30000</maxidletime></connector></connectors>< Webappsourcedirectory>${project.build.directory}/${pom.artifactid}-${pom.version}</webappsourcedirectory ><contextpath>/</contextpath></configuration></plugin></plugins></build ><properties><project.build.sourceencoding>utf-8</project.build.sourceencoding><springversion>3.1.1.release</springversion>< junitversion>3.8.1</junitversion></properties><profiles><profile><id>jdk-1.7 </id><activation><activeByDefault>true</activeByDefault><jdk>1.7</jdk>< /activation><properties><maven.compiler.source>1.7</maven.compiler.source>< maven.compiler.target>1.7</maven.compiler.target><maven.compiler.compilerversion>1.7</ maven.compiler.compilerversion></properties></profile></profiles><dependencies>< dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>${ Junitversion}</version><scope>test</scope></dependency><dependency><groupid >org.springframework</groupid><artifactid>spring-aop</artifactid><version>${ Springversion}</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId> org.springframework</groupid><artifactid>spring-asm</artifactid><version>${ Springversion}</version><type>jar</type><scope>compile</scope></dependency ><dependency><groupid>org.springframework</groupid><artifactid>spring-aspects</ Artifactid><version>${springversion}</version><type>jar</type><scope>compile </scope></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-beans</artifactid><version>${springversion}</version><type>jar </type><scope>compile</scope></dependency><dependency><groupId> org.springframework</groupid><artifactid>spring-context</artifactid><version>${ Springversion}</version><type>jar</type&gT;<scope>compile</scope></dependency><dependency><groupid>org.springframework </groupId><artifactId>spring-context-support</artifactId><version>${springversion}< /version><type>jar</type><scope>compile</scope></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-core</artifactId>< version>${springversion}</version><type>jar</type><scope>compile</scope></ Dependency><dependency><groupid>org.springframework</groupid><artifactid> Spring-expression</artifactid><version>${springversion}</version><type>jar</type ><scope>compile</scope></dependency><dependency><groupid>org.springframework </groupid><artifactid>spring-jdbc</artifactid><version>${springversion}</version ><type>jar</type><scope>compile</scope></dependency><dependency><groupid>org.springframework</ groupid><artifactid>spring-jms</artifactid><version>${springversion}</version>< Type>jar</type><scope>compile</scope></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-orm</artifactid><version>${ Springversion}</version><type>jar</type><scope>compile</scope></dependency ><dependency><groupid>org.springframework</groupid><artifactid>spring-oxm</ Artifactid><version>${springversion}</version><type>jar</type><scope>compile </scope></dependency><dependency><groupId>org.springframework</groupId>< artifactid>spring-tx</artifactid><version>${springversion}</version><type>jar</ Type><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId> spring-web</artifactid><version>${springversion}</version><type>jar</type>< scope>compile</scope></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-webmvc</artifactid><version>${springversion}</version> <type>jar</type><scope>compile</scope></dependency><dependency><groupid >org.springframework</groupid><artifactid>spring-test</artifactid><version>${ Springversion}</version><type>jar</type><scope>compile</scope></dependency ><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId> <version>1.2</version><type>jar</type><scope>compile</scope></ dependency><dependency><groupid≫commons-collections</groupid><artifactid>commons-collections</artifactid><version> 3.1</version></dependency><dependency><groupid>commons-logging</groupid>< artifactid>commons-logging</artifactid><version>1.1</version></dependency></ Dependencies></project>
6. Complete source folder information (currently only src/main/resources)
7. Set the deployment set to delete the useless Test folder.
8. Configure Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" 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_3_0.xsd "><display-name></display-name>< welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- Spring's log4j listener--><listener><listener-class>org.springframework.web.util.log4jconfiglistener </listener-class></listener><!--Core Controller--><servlet><servlet-name>test</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/ Applicationcontext.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet><listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener>< Servlet-mapping><servlet-name>test</servlet-name><url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
9. Configure Spring's Applicationcontext.xml
<?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= "com.*"/><beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >< Property name= "prefix" value= "/"/><property name= "suffix" value= ". JSP"/></bean></beans>
10. Writing Controller Classes
Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;@ Controller//This class is the path of the action on the Controller@requestmapping ("/index.do")//Map page, the path + method Path = page path public class Indexcontroller { @RequestMapping (params = "method=add") public String Add () {System.out.println ("success"); Return "Success"; Jump page}}
11. Writing the page
Operation Result:
Eclipse J2ee+maven+tomcat (Jetty) building a spring MVC project