使用MAVEN、SPRING MVC構建web 項目

1、新建一個MAVEN項目,以下圖html

2、在filter中輸入webapp篩選出maven-archetype-webapp (標準的web項目結構)java

3、填寫項目信息web

4、點擊FINISH 後可見到如下的目錄結構redis

5、添加MAVEN目錄文件夾spring

6、修改編譯路徑apache

src/main/resources  對應  target/classesjson

src/main/java  對應  target/classesapi

src/test/resources  對應  target/test-classesspring-mvc

src/test/java  對應  target/test-classessession

七:設定JRE (maven構建出來的JRE 未必符合你的需求,看狀況修改)

 

8、轉換項目類型(轉換爲標準的WEB項目並設定WEB的文件路徑)

至此項目的環境已經搭建完畢!!!!

下面開始用SPRING MVC框架來搭建應用

一、添加必要的依賴包

<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>learn.web</groupId>
	<artifactId>learn-maven-web</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>learn-maven-web Maven Webapp</name>
	<url>http://maven.apache.org</url>
	 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- spring版本號 -->
		<spring.version>4.3.0.RELEASE</spring.version>
		<spring-session.version>1.2.0.RELEASE</spring-session.version>
		<spring-data-redis.version>1.7.2.RELEASE</spring-data-redis.version>		
		<!-- log4j日誌文件管理包版本 -->
		<slf4j.version>1.7.9</slf4j.version>
		<log4j.version>2.1</log4j.version>
		<disruptor.version>3.3.4</disruptor.version>		
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.4</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.9.2</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<!-- spring核心包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>${spring-data-redis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session</artifactId>
			<version>${spring-session.version}</version>
		</dependency>		
		<!-- 導入java ee -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0-b01</version>
			<scope>provided</scope>
		</dependency>
		<!-- JSTL標籤類 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- 日誌文件管理包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.21</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>com.lmax</groupId>
			<artifactId>disruptor</artifactId>
			<version>${disruptor.version}</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.15.0-GA</version>
		</dependency>		
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.8</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>
		</dependency>		
	</dependencies>
	<build>
		<finalName>learn-maven-web</finalName>
	</build>
</project>

而後就慢慢等待依賴包下載完!

2、組織項目結構(說白了就是建文件夾來管理相應的資源文件,這個不說了,直接上圖)

三、編寫配置文件(applicationContext.xml 、springmvc-servtle.xml、web.xml)

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    	http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd"
    default-autowire="no"> 
    <!-- 自動裝配-->
    <context:annotation-config />
    <!-- 自動裝配的包路徑-->
    <context:component-scan base-package="com.niver" />
	
	<!-- 事務(註解 )-->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 
	
</beans>

springmvc-servtle.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
  		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
   		http://www.springframework.org/schema/context 
   	 	http://www.springframework.org/schema/context/spring-context-4.2.xsd
   	 	http://www.springframework.org/schema/mvc 
   	 	http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

	<!-- 設置使用註解的類所在的jar包 -->
	<context:component-scan base-package="com.niver.web" />
	<!-- Enables the Spring MVC @Controller programming model -->
	<mvc:annotation-driven />
	
    <!-- 攔截器配置,沒有的能夠不要這一段 -->
	<mvc:interceptors>
	    <!-- 使用bean定義一個Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截全部的請求 -->  
	    <bean class="com.niver.core.interceptor.CommonInterceptor"/>  
		<mvc:interceptor> 
	        <mvc:mapping path="/login/*"/> 
	        <!-- 定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的 -->  
	        <bean class="com.niver.core.interceptor.LoginInterceptor"/>  
	    </mvc:interceptor>  
	</mvc:interceptors>
	
	<!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:後綴 -->
	<mvc:view-resolvers>
	    <mvc:jsp cache-views="false" prefix="/WEB-INF/jsp/" suffix=".jsp" />  
	</mvc:view-resolvers>
	
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<description>註解controller方法適配器</description>
		<property name="messageConverters">
			<list><!--JSON轉換器 -->
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
				</bean>
			</list>
		</property>
	</bean>
	    
	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<description>配置文件上傳</description>
		<property name="defaultEncoding" value="utf-8" />
		<!-- 文件大小最大值 -->
		<property name="maxUploadSize" value="10485760000" />
		<!-- 內存中的最大值 -->
		<property name="maxInMemorySize" value="40960" />
	</bean>
</beans>

web.xml

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>Archetype Created Web Application</display-name>

	<display-name></display-name>
			
	<!-- Spring配置 -->
	<listener> 
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
	</listener> 

	<!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 -->
	<context-param> 
		<param-name>contextConfigLocation</param-name> 
		<param-value>classpath:config/applicationContext.xml</param-value> 
	</context-param>
	
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- springMVC 配置 -->
	<servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/SpringMVC-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.xhtml</url-pattern><!--這個後綴本身喜歡,哈哈哈 -->
    </servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.xhtml</welcome-file>
	</welcome-file-list>
</web-app>

而後就差很少了。

額。。。若是想在eclipse 直接運行的話,記得把編譯用到的JAR加載進去,畢竟MAVEN的包不在WEB-INF的lib下!

具體按照下圖去弄就行了!

相關文章
相關標籤/搜索