1. 程式人生 > >IntelliJ IDEA建立Spring Maven 專案

IntelliJ IDEA建立Spring Maven 專案

建立新專案

  • 建立一個新專案,Maven - Create from archetype,選中maven-archetype-webapp這個archetype

  • 輸入GroupId和ArtifactId

  • 輸入專案名稱並選擇專案存放的路徑

配置專案結構

  • 進入專案結構配置介面,File - Project Structure

  • 選中Modules
  • main下建立java資料夾;
  • webapp - WEB-INF 下建立views資料夾

  • 標記java資料夾為Sources
  • 標記resources資料夾為Resources

  • 最終的專案結構如圖

引入Spring

開啟pom.xml,引入Spring

<!--jstl-->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</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>

如果提示是否需要開啟自動匯入,選擇Enable Auto-Import,否則更改了pom.xml不會自動更新

配置web.xml

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

配置Spring

WEB-INF下新建dispatcher-servlet.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">

    <!--掃描包路徑-->
    <context:component-scan base-package="com.example" />

    <!-- 開啟註解 -->
    <context:annotation-config />
    <mvc:annotation-driven />

    <!-- 靜態資源(js、image等)的訪問 -->
    <mvc:default-servlet-handler />

    <!--ViewResolver 檢視解析器-->
    <!--用於支援Servlet、JSP檢視解析-->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

配置訪問路徑與檢視

  • 建立一個類命名HomeController,並註解@Controller,這個類就會被當成Controller
  • 再加上訪問路徑的註解@RequestMapping("/home")
  • 在HomeController內建立一個hello方法,在方法上註解@RequestMapping("/hello"),這樣當訪問http://localhost:8080/spring-helloworld/home/hello的時候,就會執行這個方法
  • 最後return "hello",這樣執行完hello方法後會跳轉到hello.jsp(hello.jsp需要建在WEB-INF-views下)

配置Tomcat

新增一個Tomcat服務 
+ - Tomcat Server - Local

切換到Deployment+ - Artifact...添加當前專案的war,並設定專案訪問路徑為spring-helloworld

執行Tomcat

執行完成後,在瀏覽器訪問http://localhost:8080/spring-helloworld/home/hello,就進入到hello.jsp了

*關於使用JSP

如果需要在jsp使用el表示式taglib,那還需要在pom.xml引入相應的庫

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

並且在jsp的第一行加入isELIgnored = false,否則el表示式不會生效

<%@page isELIgnored="false" %>