您好,登錄后才能下訂單哦!
約定電腦都安裝了eclipse,且已配置好Maven以及eclipse插件。
1.Eclipse
2.maven
3.Eclipse 需要安裝maven插件。url:maven - http://download.eclipse.org/technology/m2e/releases 。
1、新建一個(gè)Maven Project
2、選擇工作空間
3、搭建Web工程,我們選擇maven-archetype-webapp類型
4、填寫項(xiàng)目參數(shù),如圖
5、以上步驟完成時(shí)的工程結(jié)構(gòu)目錄
6、可以查看或修改發(fā)布目錄
7、確保勾選上Dynamic Web Module和Java
8、完成以上步驟,我們的工程就是一個(gè)Web項(xiàng)目了,接著我們賦予工程的springmvc特性,配置web.xml,使其具有springmvc特性,主要配置兩處,一個(gè)是ContextLoaderListener,一個(gè)是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"> <!-- 配置web.xml,使其具有springmvc特性,主要配置兩處,一個(gè)是ContextLoaderListener,一個(gè)是DispatcherServlet --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- 配置ContextLoaderListener表示,該工程要以spring的方式啟動(dòng)。啟動(dòng)時(shí)會(huì)默認(rèn)在/WEB-INF目錄下查找applicationContext.xml 作為spring容器的配置文件,該文件里可以初始化一些bean --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符集過(guò)濾器 --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring mvc 配置 --> <!-- 配置DispatcherServlet表示,該工程將采用springmvc的方式。啟動(dòng)時(shí)也會(huì)默認(rèn)在/WEB-INF目錄下查找XXX-servlet.xml作為配置文件, XXX就是DispatcherServlet的名字,該文件中將配置兩項(xiàng)重要的mvc特性:HandlerMapping,負(fù)責(zé)為DispatcherServlet這個(gè)前端控制器的請(qǐng)求查找Controller; ViewResolver,負(fù)責(zé)為DispatcherServlet查找ModelAndView的視圖解析器。 此處使用指定的配置文件spring-mvc.xml --> <servlet> <servlet-name>contacts</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value>--> <param-value>classpath*:/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>contacts</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
9、配置ContextLoaderListener表示,該工程要以spring的方式啟動(dòng)。啟動(dòng)時(shí)會(huì)默認(rèn)在/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"> <!-- bean definitions here --> <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 /> <!-- 此行語(yǔ)句使得resource autowired 等四個(gè)注解可以使用 --> <!-- 實(shí)例化jdbcTemplate,同時(shí)注入數(shù)據(jù)源 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" /> <!-- 配置事務(wù)管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事務(wù)掃描開(kāi)始(開(kāi)啟@Tranctional) --> <tx:annotation-driven transaction-manager="txManager" /> </beans>
10、配置DispatcherServlet表示,該工程將采用springmvc的方式。啟動(dòng)時(shí)也會(huì)默認(rèn)在/WEB-INF目錄下查找XXX- servlet.xml作為配置文件,XXX就是DispatcherServlet的名字,該文件中將配置兩項(xiàng)重要的mvc特性:ViewResolver,負(fù)責(zé)為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"> <!-- 啟動(dòng)事務(wù)添加配置 --> <context:component-scan base-package="cn.jxufe"> <!-- 排除掃描service標(biāo)簽 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <!-- 注冊(cè)JASON轉(zhuǎn)換器 --> <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-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </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> <!-- 當(dāng)配置了<mvc:annotation-driven/>后,會(huì)自動(dòng)加載org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping和 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter,所以如果顯示的配置則需去掉配置<mvc:annotation-driven/> --> <!--<mvc:annotation-driven/>--> </beans>
11、配置pom.xml文件,讓maven自動(dòng)配置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.0</modelVersion> <groupId>cn.jxufe</groupId> <artifactId>SpringMVC-Maven</artifactId> <packaging>war</packaging> <version>1.0</version> <name>SpringMVC-Maven Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- web jar --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <!-- jackson jar --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.11</version> </dependency> <!-- commons jar --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-chain</groupId> <artifactId>commons-chain</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math4</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-digester3</artifactId> <version>3.2</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- jstl jar --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- 日志相關(guān) --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!-- 驗(yàn)證碼kaptcha --> <dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency> </dependencies> <build> <finalName>SpringMVC-Maven</finalName> </build> </project>
12、完成以上步驟后,可以繼續(xù)配置jdbc.properties、log4j.properties等文件。至此SpringMVC項(xiàng)目搭建完成,工程結(jié)構(gòu)如下圖:
13、代碼我都會(huì)托管到GibHub上,有需要的朋友可以查看。GitHub
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。