環境構成--IDEAは、Mavenの+ Spring MVCの開発環境を構築します

1.新しいプロジェクトを作成し、Mavenのプロジェクトを選択し、下図のようにオプションを選択し、次の

2.のgroupIdとたartifactIdを塗りつぶし、これはMavenのリポジトリと設定の一意性を決定するために、Mavenプロジェクトです。groupIdを一般的に記述されたドメイン名、会社名は、たartifactIdはプロジェクト名です。

3. Mavenのプロフィールアドレスを選択し、あなたがMavenをダウンロードして選択し、設定ファイルの倉庫アドレスをカスタマイズすることができ、あなたはすべてのものは、Cドライブに積まれていません。そして、アリのクラウドストレージへのリモートリポジトリのアドレスは、そのダウンロード速度が速くなります。

4.次に、すべての方法次、プロジェクトのmavenの構築を完了します。来春MVCの構成環境、右プロジェクト名、春のMVCを選択し、フレームワークのサポートを追加]を選択し、コンパイラは、Spring MVCの環境の構築を開始します。

5.のpom.xmlに春の依存関係を追加します。

<!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>

6.配置web.xml,Spring IoC上下文配置文件appliacitonContext.xml和映射请求上下文配置文件dispatcher-servlet.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="4.0">
  <!--配置SpringIoC容器的配置文件路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <!--配置ContextLoaderListener用以初始化Spring IoC容器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--配置DispatcherServlet-->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <!--servlet拦截配置,拦截以后缀“form”结束的请求-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

applicationContext.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:tx="http://www.springframework.org/schema/tx"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!--使用注解驱动-->
    <context:annotation-config/>


</beans>

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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
       http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!--使用注解驱动-->
    <mvc:annotation-driven/>
    <!--定义扫描装载的包-->
    <context:component-scan base-package="com.leran.example.*"/>
    <!--定义视图解析器-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

    <!--
    <tx:annotation-driven transaction-manager="transactionManager"/>
    -->
</beans>

8. 接着配置tomcat服务器,右上角Add Configuration,点左上角的加号,选择tomcat server,local,进入tomcat配置页面,选择deployment,添加Artifact,选择第一个就可以了

9.现在环境就配完了,写一个controller测试一下,工程结构配置如图,注意test.jsp的位置,和diapatcher-servlet.xml中的配置位置是相对应的

 10.写的不是特别完善,如有问题还请指正

 

おすすめ

転載: www.cnblogs.com/yingying7/p/11287354.html