構建web應用之——maven建立以及SpringMVC配置

構建web應用第一步須要建立以及配置maven項目管理,同時配置啓動SpringMVC,這裏推薦參考CSDN的一篇文章
連接:https://blog.csdn.net/weixin_42222334/article/details/80362126,
下面列出一些要點css

1、打開IDEA建立新項目
1.工具欄左側選擇maven(應爲要建立maven項目管理)
2.選擇本身本地已有的jdk
3.勾選create from archetype(很重要)
4.選擇org.apache.maven.archetypes:maven-archetype-webapp(還有一個webapp,別選錯了)
5.ArtifactId最好與項目名稱一致(我把GourpId也命名成了項目ID)
2、配置maven
1.最好使用本身本地的maven(沒有的話能夠去官網下載,下載好後還要配置環境變量)
2.將自已下好的maven目錄導入maven home directory
3.user setting file爲你的maven中settings.xml文件位置,通常在conf目錄中
4.Local Repository爲你的maven庫所在位置(經過settings.xml查看和配置)
5.開始建立過程當中,IDE右下角會彈出一個maven項目須要被導入的對話框,點擊enable auto-import使其自動導入
6.終端中顯示maven execution finished則表明maven建立成功
3、配置SpringMVC
1.在pom.xml中添加相關依賴,這裏只是一些基本依賴,後續還會加入其它依賴java

 

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 日誌 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.8.0-beta2</version>
    </dependency>

    <!-- J2EE -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version><!--配置web.xml要使用的版本-->
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1-b03</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- springframework -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
  </dependencies>

2.添加SpringMVC框架:項目右鍵-添加框架支持,勾選Spring以及SpringMVC
若沒有Spring,表明咱們的項目中已經存在Spring(但不必定完整)。經過File-project structure-facets
找到Spring,並右鍵刪除後再添加
3.配置web.xml,dispatcher-servlet.xml,applicationContext.xml(核心步驟)web

web.xmlspring

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
    
    <!-- 配置上下文參數,將applicationContext.xml配置進來-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 註冊ContextLoaderListener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 註冊DispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定DispatcherServlet配置文件的位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- 配置DispatcherServlet映射 -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

dispatcher-servlet.xmlapache

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!-- 負責整個mvc中的配置-->

    <!-- 啓用spring的註解 -->
    <context:annotation-config/>

    <!-- 配置註解驅動,要使用註解必需要配置 -->
    <mvc:annotation-driven/>

    <!-- 靜態資源映射-->
    <!-- 把靜態資源放在webapp的statics目錄下-->
    <mvc:resources mapping="/css/**" location="statics/css/"/>
    <mvc:resources mapping="/js/**" location="statics/js/"/>
    <mvc:resources mapping="/image/**" location="statics/images/"/>
    <mvc:default-servlet-handler/><!-- 配置靜態資源的處理,實現解耦,很關鍵 -->

    <!-- 配置視圖解析器-->
    <!-- InternalResourceViewResolver最爲簡單,解析jsp
        若要使用jstl標籤處理,則須要將InternalResourceViewResolver解析爲jstlView-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 自動掃描裝配 -->
    <context:component-scan base-package="example.controller"/>
</beans>

 

applicationContext.xmlapi

<?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"
       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">
    <!-- 負責mvc組建的配置 -->
    <context:component-scan base-package="example"/>
</beans>

注意:在使用context,mvc等spring框架下的標籤時,須要同時聲明庫以及庫解析器(mvc, spring-mvc.xsd)不然會報告500狀態碼spring-mvc

4、配置tomcat
1.點擊Run/edit configuration配置tomcat(先配置development後配置server)
2.選擇artifacts,選擇末尾帶expluded的那個(很重要)
3.在server中吧on update action以及on frame deactivation都選擇update classes and resources(不用從新啓動tomcat,每次只需刷新就可看到頁面變化)tomcat

整個項目結構:mvc

相關文章
相關標籤/搜索