约定电脑都安装了eclipse,且已配置好Maven以及eclipse插件。
1.Eclipse
2.maven
3.Eclipse 需要安装maven插件。url:maven - http://download.eclipse.org/technology/m2e/releases 。
1、新建一个Maven Project
2、选择工作空间
3、搭建Web工程,我们选择maven-archetype-webapp类型
4、填写项目参数,如图
5、以上步骤完成时的工程结构目录
6、可以查看或修改发布目录
7、确保勾选上Dynamic Web Module和Java
8、完成以上步骤,我们的工程就是一个Web项目了,接着我们赋予工程的springmvc特性,配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet。代码如下:
xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath*:applicationContext.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <filter> <filter-name>encodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>encodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet> <servlet-name>contactsservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath*:/spring-mvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>contactsservlet-name> <url-pattern>*.htmurl-pattern> servlet-mapping> <welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list> web-app>
9、配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找 applicationContext.xml作为spring容器的配置文件,这里可以初始化一些bean,如DataSource等。代码如下:
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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <context:component-scan base-package="cn.jxufe" /> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <context:annotation-config /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" /> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> bean> <tx:annotation-driven transaction-manager="txManager" /> beans>
10、配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX- servlet.xml作为配置文件,XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。这里我们使用指定的xml配置文件spring-mvc.xml,代码如下:
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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.2.xsd" default-lazy-init="true"> <context:component-scan base-package="cn.jxufe"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> context:component-scan> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <ref bean="utf8StringHttpMessageConverter"/> list> property> bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8value> <value>text/html;charset=UTF-8value> <value>application/json;charset=UTF-8value> list> property> bean> <bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8value> <value>text/html;charset=UTF-8value> <value>application/json;charset=UTF-8value> list> property> bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> bean> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">bean> beans>
11、配置pom.xml文件,让maven自动配置jar包,代码如下
<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.0modelVersion> <groupId>cn.jxufegroupId> <artifactId>SpringMVC-MavenartifactId> <packaging>warpackaging> <version>1.0version> <name>SpringMVC-Maven Maven Webappname> <url>http://maven.apache.orgurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <spring.version>3.1.2.RELEASEspring.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.0.1version> <scope>providedscope> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>1.1.2version> <scope>providedscope> dependency> <dependency> <groupId>javax.servlet.jspgroupId> <artifactId>javax.servlet.jsp-apiartifactId> <version>2.3.1version> <scope>providedscope> dependency> <dependency> <groupId>org.codehaus.jacksongroupId> <artifactId>jackson-jaxrsartifactId> <version>1.9.11version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>3.3.2version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.4version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-collections4artifactId> <version>4.0version> dependency> <dependency> <groupId>commons-logginggroupId> <artifactId>commons-loggingartifactId> <version>1.1.3version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>1.8version> dependency> <dependency> <groupId>commons-beanutilsgroupId> <artifactId>commons-beanutilsartifactId> <version>1.8.3version> dependency> <dependency> <groupId>commons-chaingroupId> <artifactId>commons-chainartifactId> <version>1.2version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3.1version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-math3artifactId> <version>3.3version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-pool2artifactId> <version>2.2version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-digester3artifactId> <version>3.2version> dependency> <dependency> <groupId>commons-netgroupId> <artifactId>commons-netartifactId> <version>3.3version> dependency> <dependency> <groupId>commons-dbutilsgroupId> <artifactId>commons-dbutilsartifactId> <version>1.5version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-emailartifactId> <version>1.3.3version> dependency> <dependency> <groupId>commons-dbcpgroupId> <artifactId>commons-dbcpartifactId> <version>1.4version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>taglibsgroupId> <artifactId>standardartifactId> <version>1.1.2version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>1.2.16version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>1.7.5version> dependency> <dependency> <groupId>com.google.codegroupId> <artifactId>kaptchaartifactId> <version>2.3.0version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.31version> dependency> dependencies> <build> <finalName>SpringMVC-MavenfinalName> build> project>
12、完成以上步骤后,可以继续配置jdbc.properties、log4j.properties等文件。至此SpringMVC项目搭建完成,工程结构如下图:
13、今后博客中的代码我都会托管到GibHub上,有需要的朋友可以查看。GitHub