IDEA 建立 Spring MVC 詳細教程-- 1
阿新 • • 發佈:2018-12-16
一、使用DEA建立專案
- 新建專案file-new -project
2.新建一個maven project,並且選擇webapp原型。
3.填寫GroupId和ArtifactId,
GroupID 是專案組織唯一的識別符號,實際對應JAVA的包的結構,是main目錄裡java的目錄結構,一般域名+公司名。
ArtifactID是專案的唯一的識別符號,實際對應專案的名稱,就是專案根目錄的名稱。
4.專案目錄結構
5.根據對專案的任意目錄進行這五種目錄型別標註。
6. 包結構設定
二 、搭建專案
- 準備好之後,開始配置SpringMVC ,先是配置 pom.xml檔案。
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hf</groupId> <artifactId>springmvc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 標記庫用於檢視層的支援 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> <scope>runtime</scope> </dependency> <!--spring相關包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.1.RELEASE</version> </dependency> </dependencies> <build> <finalName>springmvc</finalName> <resources> </resources> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2. 接下來配置web 檔案,web檔案在啟動時被載入,同時載入其中配置的 其他xml 檔案。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app 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_3_0.xsd " version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- 配置引用spring-mvc.xml --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 啟動時載入spring-mvc.xml --> <param-value>classpath:spring-mvc.xml</param-value> <!--<param-value>/WEB-INF/spring-mvc.xml</param-value>--> </init-param> <!-- 啟動時載入spring-mvc.xml 的優先順序高--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在web.xml 配置中設定spring-servlet.xml的載入路徑 <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> 上面的這個是可以不用配置的,框架預設在/WEB-INF/下 自己掃描對應的XXX--mvc.xml配置檔案,也可以自己指定;同時也可把配置檔案指定到 resources 目錄下,本例則是放在resources 目錄下。
3. 配置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: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.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 ">
<!-- 開啟spring的掃描注入,使用如下註解 -->
<!-- @Component,@Repository,@Service,@Controller-->
<!-- 錯誤點 com.hf.controller 不能掃描到service ,dao等-->
<context:component-scan base-package="com.hf"/>
<!-- 開啟springMVC的註解驅動,使得url可以對映到對應的controller -->
<mvc:annotation-driven />
<!-- 檢視解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注意點:新建專案需要引入下圖紅框中的連結,用於支援MVC註解驅動。
<!-- 開啟springMVC的註解驅動,使得url可以對映到對應的controller --> <mvc:annotation-driven />