如何在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吧!
-
你見過哪些有趣的動物?動物15大有趣的科普知識
導語:動物中也有不少我們不知道的冷知識,比如蝦心臟在頭部;蝸牛能睡三年;鼻涕蟲有四個鼻子;大象是唯一不能跳躍的動物;南極冰川中近百分之三的冰是企鵝尿;蝙蝠離開洞穴時總是向左拐;長頸鹿沒有聲帶;袋鼠不會放屁;大約50%的猩猩骨頭骨折;鴕鳥的眼睛比它的大腦大,之前小編爲...
-
刺客衣裝怎麼製作
(01)刺客衣裝的製作方法因遊戲和版本而異,以下是一些常見的遊戲和製作方法:1.《刺客信條》系列遊戲:在《刺客信條》系列遊戲中,玩家可以透過商店購買或完成任務獲得刺客衣裝。(02)例如,在《刺客信條2》中,玩家可以在佛羅倫薩的商店中購買刺客衣裝,也可以完成特定的任務來...
-
鬼月懷孕有什麼說法?7月鬼月出生的孩子好嗎(鬼胎/天胎)
導語:在中國傳統中將農曆七月稱爲鬼月,傳說七月開地獄之門,鬼魂就會來到人間,那麼農曆7月受孕好不好呢?鬼月適宜懷孕嗎?七月的禁忌有哪些呢?很多人都有着這樣那樣的問題,下面和探祕志小編一起了解一下吧。7月鬼月出生的孩子好嗎在民間有兩種截然不同的說法,一種是說...
-
酒能寄快遞嗎
(01)酒在寄快遞方面需要考慮以下因素:1.國家法律法規:不同國家對於寄送酒類的規定存在差異,一些國家可能允許寄送酒類,但也有些國家可能禁止或限制寄送酒類。(02)在寄送酒類之前,需要先了解目的地國家的法律法規和郵寄政策。2.郵寄公司政策:郵寄公司通常有自己的規定和限...