如何在Intelli IDEA中優雅的建立SpringMVC專案
本文講述如何在IntelliIDEA中,快速優雅的建立SpringMVC專案。
操作方法
(01)首先,我們選擇新建,然後空專案,填入你想要的名字即可,如圖所示。這裡選擇空專案是為了後續,我們可以擴充套件新的模組進去。正常情況下,新建完這個空專案,會自動彈出新增的模組的對話方塊,如圖示。新建
(02)來到新增模組的介面,選擇如圖示的+,並在下啦列表中選擇New Module,來到建立模組的介面。
(03)此處我們以新建maven模組為例。選擇maven,勾選archetype,並從可選項中選擇webapp,直接下一步。
(04)填寫好對應的資訊,一步到底即可。然後idea會自動建立一個基礎工程目錄出來。我們可以看到有pom檔案,資源目錄等。
(05)根據我個人的開發習慣,還要做一系列的調整。一,選擇開發語言等級;二,建立Java根目錄,這裡將用來編寫Java程式碼。
(06)接下來就是編碼了。web.xml作為tomcat啟動專案的入口,會自動建立。然後我們還要手動建立一個SpringMVC的xml檔案。這裡我直接從其他專案拷貝一個檔案過來作為示例說明。不過在這之前,我們得先新增springmvc的依賴。<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version></dependency>注意,如果只是簡單的SpringMVC專案,那麼僅此一個依賴項就夠了。如果涉及到aop orm 或者spring的其他特效,我們再按需要新增其他依賴。
(07)<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 靜態資源預設servlet配置 1、加入對靜態資源的處理:js,css,gif,png 2、允許使用"/"做整體對映 --> <mvc:default-servlet-handler/> <!-- 開啟註解 --> <mvc:annotation-driven /> <context:annotation-config /> <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.zjoin.story.admin.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--避免IE執行AJAX時,返回JSON出現下載檔案 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON轉換器 --> </list> </property> </bean> <!-- 定義跳轉的檔案的前後綴 ,檢視模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 --> <property name="prefix" value="/WEB-INF/html/"/> <property name="suffix" value=".html"/> </bean> <!-- 開啟AOP攔截 --> <!--<aop:aspectj-autoproxy proxy-target-class="true"/>--></beans>--------------------------------------------------------------------------------以上是配置全文,大家根據自己的實際情況修改包路徑即可。
(08)<?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>admin</display-name> <welcome-file-list> <welcome-file>/WEB-INF/html/index.html</welcome-file> </welcome-file-list> <!-- Spring和mybatis的配置檔案 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-*.xml</param-value> </context-param> <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*:springMvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- SpringMVC攔截設定 --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 由SpringMVC攔截所有請求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- SpringMVC攔截設定結束 --> <!-- Spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 編碼過濾器 --> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>--------------------------------------------------------------------------------這裡有個小細節,可以注意到springMvc和我引用的其他spring檔案命名格式不一樣,這是為了單獨載入SpringMVC檔案,而不會出現重複載入的情況。
(09)到這裡差不多就配置好了,接下去去編寫你的Controller吧!
-
萬人血書是什麼意思
(01)“萬人血書”通常是一種公開向政府或其他有權力的機構或組織發表的一種宣告,形式通常是由許多人簽名的一份檔案。(02)這種宣告可能是要求某個問題得到解決,或對某個政策或事件進行抗議。通過將眾多人的簽名集結起來,這種宣告能夠表達更加廣泛的聲音和力量。“血書...
-
柔式按摩是什麼意思
(01)柔式按摩是一種按摩手法,其主要特點是柔和、舒適、放鬆,通過按摩技師的手法,使接受按摩的人感到輕鬆、舒適,從而達到舒緩疲勞、緩解肌肉緊張、消除疼痛等效果。(02)柔式按摩強調以柔克剛,通過柔和的手法達到舒緩肌肉、解除疲勞的目的。柔式按摩一般採用推、按、捏、...
-
鹽析法沉澱蛋白質的原理
(01)除了上述提到的鹽析法沉澱蛋白質的原理之外,還有其他一些方法可以用來沉澱蛋白質。以下是一些常見的方法:1.酸鹼法:通過調整溶液的酸鹼度,使蛋白質的溶解度發生變化,從而實現蛋白質的沉澱。酸鹼法通常用於沉澱那些在特定pH值下不穩定的蛋白質。(02)2.有機溶劑法:某些...
-
指甲月白代表什麼?健康與月牙白有關係嗎(正常代謝)
導語:指甲月白又被稱為月牙白,就是指手指甲上的半月形白色痕跡,一般在指甲下方五分之一處,只是人體正常代謝的產物,手指上沒有血管流經的地方,就會產生白月牙,不過在中醫看來,月牙代表著一個人的氣血情況,因為熬夜過度生活不規律,會導致身體精力不足月牙白少,下面就跟著探...