Springmvc相關配置總結

m0_49294021發表於2020-09-27

文章目錄

  1. Springmvc相關配置總結

    1.Spring相關的依賴
    	1.Spring基礎的依賴
    		1.先加依賴 ----放在pom.xml 中的<dependencies>  之間</dependencies>
    		    <!--Spring基本依賴(三個)-->
    				<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    				<dependency>
    				  <groupId>org.springframework</groupId>
    				  <artifactId>spring-core</artifactId>
    				  <version>5.1.5.RELEASE</version>
    				</dependency>
    				
    				<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    				<dependency>
    				  <groupId>org.springframework</groupId>
    				  <artifactId>spring-beans</artifactId>
    				  <version>5.1.5.RELEASE</version>
    				</dependency>
    				
    				<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    				<dependency>
    				  <groupId>org.springframework</groupId>
    				  <artifactId>spring-context</artifactId>
    				  <version>5.1.5.RELEASE</version>
    				</dependency>
    			
    	2.切面程式設計依賴----註解實現AOP的相關依賴
    		pom.xml加入依賴 放在pom.xml 中的<dependencies>  之間</dependencies>
    		1.額外的加依賴(兩個依賴)
    			<!--切面(兩個)-->
    				<dependency>
    				  <groupId>org.aspectj</groupId>
    				  <artifactId>aspectjrt</artifactId>
    				  <version>1.8.13</version>
    				</dependency>
    
    				<dependency>
    				  <groupId>org.aspectj</groupId>
    				  <artifactId>aspectjweaver</artifactId>
    				  <version>1.8.13</version>
    				</dependency>		
    			
    	3.SpringMVC依賴--匯入jar包(匯入依賴)
    			pom.xml加入依賴  放在pom.xml 中的<dependencies>  之間</dependencies>
    			
    			<!--SpringMVC依賴(兩個)-->
    				<dependency>
    					<groupId>org.springframework</groupId>
    					<artifactId>spring-webmvc</artifactId>
    					<version>5.1.5.RELEASE</version>
    				</dependency>
    				
    				<dependency>
    					<groupId>javax.servlet</groupId>
    					<artifactId>javax.servlet-api</artifactId>
    					<version>3.1.0</version>
    				</dependency>		
    	
    	4.JSTL---依賴
    		<!--JSTL依賴(兩個)-->
    		<dependency>
    			<groupId>javax.servlet.jsp.jstl</groupId>
    			<artifactId>jstl-api</artifactId>
    			<version>1.2</version>
    		</dependency>
    		
    		<!-- https://mvnrepository.com/artifact/taglibs/standard -->
    		<dependency>
    		    <groupId>taglibs</groupId>
    		    <artifactId>standard</artifactId>
    		    <version>1.1.2</version>
    		</dependency>
    
    	5.檔案上傳 -- 加依賴(圖片)(*****)
    		<!--檔案上傳依賴(兩個)-->
    		<dependency>
    		  <groupId>commons-fileupload</groupId>
    		  <artifactId>commons-fileupload</artifactId>
    		  <version>1.3.3</version>
    		</dependency>
    
    		<dependency>
    		  <groupId>commons-io</groupId>
    		  <artifactId>commons-io</artifactId>
    		  <version>2.4</version>
    		</dependency>
    
    
    
    
    	
    2.web.xml的相關配置
    	1.編寫web.xml配置檔案---配置前端控制器
    			<!--配置前端控制器-->
    			<servlet>
    				<servlet-name>spring</servlet-name>
    				<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    				
    				<!--初始化資訊,載入springmvc.xml配置檔案-->
    				<init-param>
    				  <param-name>ContextConfigLocation</param-name>
    				  <param-value>Classpath:spring.xml</param-value>
    				</init-param>
    			</servlet>
    
    			<servlet-mapping>
    				<servlet-name>spring</servlet-name>
    				<url-pattern>/</url-pattern>
    			</servlet-mapping>
    			
    			<!-- 配置解決中文亂碼的過濾器-->
    			<filter>
    				<filter-name>CharacterEncodingFilter</filter-name>
    				<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    				
    				<!--初始化資訊,載入UTF-8配置檔案-->
    				<init-param>
    				  <param-name>encoding</param-name>
    				  <param-value>UTF-8</param-value>
    				</init-param>
    			</filter>
    			<filter-mapping>
    				<filter-name>CharacterEncodingFilter</filter-name>
    				<url-pattern>/*</url-pattern>
    			</filter-mapping>
    			
    			
    3.在resources裡面寫一個配置檔案 -- springmvc.xml			
    	1.載入標頭檔案
    		<?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:aop="http://www.springframework.org/schema/aop"
    			   xsi:schemaLocation="
    				http://www.springframework.org/schema/beans
    				http://www.springframework.org/schema/beans/spring-beans.xsd
    				http://www.springframework.org/schema/context
    				http://www.springframework.org/schema/context/spring-context.xsd
    				http://www.springframework.org/schema/aop
    				http://www.springframework.org/schema/aop/spring-aop.xsd">
    				
    		</beans>		
    
    				1.//開啟註解掃描 -- 只寫這一個就行,以後的開發基本上都是用註解的形式
    				<context:component-scan base-package="controller"></context:component-scan>
    				
    				2. <!-- 定義檔案上傳解析器 -->
    				<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    					<!-- 設定預設編碼 -->
    					<property name="defaultEncoding" value="UTF-8"></property>
    					<!-- 設定檔案上傳的最大值為5MB,5*1024*1024 -->
    					<property name="maxUploadSize" value="5242880"></property>
    					<!-- 設定檔案上傳時寫入記憶體的最大值,如果小於這個引數不會生成臨時檔案,預設為10240 -->
    					<property name="maxInMemorySize" value="40960"></property>
    					<!-- 上傳檔案的臨時路徑 -->
    					 <property name="uploadTempDir" value="fileUpload/temp"></property>
    					<!-- 延遲檔案解析 -->
    					<property name="resolveLazily" value="true"/>
    				</bean>
    				
    				3.<!--配置攔截器-->
    				<mvc:interceptors>
    					<mvc:interceptor>
    						<mvc:mapping path="/**"><!--要攔截的路徑-->
    							<bean class=""></bean><!--用誰做攔截這個事-->
    						</mvc:mapping>
    					</mvc:interceptor>
    				</mvc:interceptors>
    
    				
    
    		</beans>
    			
    			
    	2.注意:idea預設忽略el表示式,下面是開啟不忽略EL表示式的方法
    			編寫web.xml配置檔案
    				<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-app>	
    

相關文章