中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Maven搭建SpringMVC項目的步驟(圖文教程)

發布時間:2020-10-08 12:02:55 來源:腳本之家 閱讀:149 作者:何海洋 欄目:編程語言

約定電腦都安裝了eclipse,且已配置好Maven以及eclipse插件。

  1.Eclipse

  2.maven

  3.Eclipse 需要安裝maven插件。url:maven - http://download.eclipse.org/technology/m2e/releases 。

1、新建一個Maven Project

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 2、選擇工作空間

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 3、搭建Web工程,我們選擇maven-archetype-webapp類型

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 4、填寫項目參數,如圖

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 5、以上步驟完成時的工程結構目錄

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 6、可以查看或修改發布目錄

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 7、確保勾選上Dynamic Web Module和Java

使用Maven搭建SpringMVC項目的步驟(圖文教程)

 8、完成以上步驟,我們的工程就是一個Web項目了,接著我們賦予工程的springmvc特性,配置web.xml,使其具有springmvc特性,主要配置兩處,一個是ContextLoaderListener,一個是DispatcherServlet。代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
  
  <!-- 配置web.xml,使其具有springmvc特性,主要配置兩處,一個是ContextLoaderListener,一個是DispatcherServlet -->
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置ContextLoaderListener表示,該工程要以spring的方式啟動。啟動時會默認在/WEB-INF目錄下查找applicationContext.xml
作為spring容器的配置文件,該文件里可以初始化一些bean -->
  <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>
    <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>
  
  <!-- spring mvc 配置 -->
  <!-- 配置DispatcherServlet表示,該工程將采用springmvc的方式。啟動時也會默認在/WEB-INF目錄下查找XXX-servlet.xml作為配置文件,
    XXX就是DispatcherServlet的名字,該文件中將配置兩項重要的mvc特性:HandlerMapping,負責為DispatcherServlet這個前端控制器的請求查找Controller;
    ViewResolver,負責為DispatcherServlet查找ModelAndView的視圖解析器。
    此處使用指定的配置文件spring-mvc.xml -->
  <servlet>
    <servlet-name>contacts</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value>-->
      <param-value>classpath*:/spring-mvc.xml</param-value>
    </init-param> 
    <load-on-startup>1</load-on-startup> 
  </servlet>
  <servlet-mapping>
    <servlet-name>contacts</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>

9、配置ContextLoaderListener表示,該工程要以spring的方式啟動。啟動時會默認在/WEB-INF目錄下查找 applicationContext.xml作為spring容器的配置文件,這里可以初始化一些bean,如DataSource等。代碼如下:

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

  <!-- bean definitions here -->
  <context:component-scan base-package="cn.jxufe" />
  
  <!-- 配置讀取外部配置文件 -->
  <context:property-placeholder location="classpath:jdbc.properties" />
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
  <context:annotation-config /> <!-- 此行語句使得resource autowired 等四個注解可以使用 -->
  
  <!-- 實例化jdbcTemplate,同時注入數據源 -->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    p:dataSource-ref="dataSource" />
  
  <!-- 配置事務管理器 -->
  <bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  
  <!-- 事務掃描開始(開啟@Tranctional) -->
  <tx:annotation-driven transaction-manager="txManager" />

</beans>

 10、配置DispatcherServlet表示,該工程將采用springmvc的方式。啟動時也會默認在/WEB-INF目錄下查找XXX- servlet.xml作為配置文件,XXX就是DispatcherServlet的名字,該文件中將配置兩項重要的mvc特性:ViewResolver,負責為DispatcherServlet查找ModelAndView的視圖解析器。這里我們使用指定的xml配置文件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:p="http://www.springframework.org/schema/p"  
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-3.0.xsd  
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
default-lazy-init="true">

  <!-- 啟動事務添加配置 -->
  <context:component-scan base-package="cn.jxufe"> 
    <!-- 排除掃描service標簽 -->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>  
  </context:component-scan> 
  
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <!-- 注冊JASON轉換器 -->
    <property name="messageConverters"> 
      <list> 
        <ref bean="mappingJacksonHttpMessageConverter"/> 
        <ref bean="utf8StringHttpMessageConverter"/>
      </list> 
    </property> 
  </bean>
  
  <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
    <property name="supportedMediaTypes"> 
      <list> 
        <value>text/plain;charset=UTF-8</value>
        <value>text/html;charset=UTF-8</value> 
        <value>application/json;charset=UTF-8</value>
      </list> 
    </property> 
  </bean> 
  <bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="supportedMediaTypes">
      <list>
        <value>text/plain;charset=UTF-8</value>
        <value>text/html;charset=UTF-8</value>
        <value>application/json;charset=UTF-8</value>
      </list>
    </property>
  </bean>

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
 
  
  <!-- 當配置了<mvc:annotation-driven/>后,會自動加載org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping和
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter,所以如果顯示的配置則需去掉配置<mvc:annotation-driven/> -->
  <!--<mvc:annotation-driven/>-->
</beans>

11、配置pom.xml文件,讓maven自動配置jar包,代碼如下

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.jxufe</groupId>
  <artifactId>SpringMVC-Maven</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>SpringMVC-Maven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.1.2.RELEASE</spring.version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- spring begin -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- spring end -->

    <!-- web jar -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- jackson jar -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-jaxrs</artifactId>
      <version>1.9.11</version>
    </dependency>

    <!-- commons jar -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.3.2</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.0</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.3</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.8</version>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupId>commons-chain</groupId>
      <artifactId>commons-chain</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-math4</artifactId>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-digester3</artifactId>
      <version>3.2</version>
    </dependency>
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupId>commons-dbutils</groupId>
      <artifactId>commons-dbutils</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-email</artifactId>
      <version>1.3.3</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- jstl jar -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!-- 日志相關 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>

    <!-- 驗證碼kaptcha -->
    <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3.0</version>
    </dependency>
    <!-- mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.31</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVC-Maven</finalName>
  </build>
</project>

12、完成以上步驟后,可以繼續配置jdbc.properties、log4j.properties等文件。至此SpringMVC項目搭建完成,工程結構如下圖:

使用Maven搭建SpringMVC項目的步驟(圖文教程)

13、代碼我都會托管到GibHub上,有需要的朋友可以查看。GitHub

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

龙南县| 淳安县| 建德市| 进贤县| 康平县| 中超| 周至县| 永平县| 峡江县| 博爱县| 涡阳县| 电白县| 探索| 股票| 长丰县| 鹤庆县| 聂拉木县| 房山区| 广南县| 会理县| 赫章县| 大方县| 江门市| 翁源县| 金华市| 常熟市| 托里县| 永登县| 加查县| 邵阳市| 青田县| 镇宁| 商丘市| 海阳市| 东港市| 工布江达县| 盘山县| 罗平县| 通榆县| 镇巴县| 岢岚县|