美文网首页
【一】使用IDEA Maven 搭建SpringMVC环境

【一】使用IDEA Maven 搭建SpringMVC环境

作者: 花季浅忆 | 来源:发表于2019-03-08 09:56 被阅读0次

    1.创建maven工程


    1.png 2.png

    然后一路Next即可创建好后的项目目录如下:


    3.png

    2.添加相关依赖

    <!--测试-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <!--日志-->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.21</version>
        </dependency>
        <!--J2EE-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.2</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <!--mysql驱动包-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.35</version>
        </dependency>
        <!--springframework-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>4.2.6.RELEASE</version>
        </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-jdbc</artifactId>
              <version>4.2.6.RELEASE</version>
          </dependency>
        <dependency>
          <groupId>com.github.stefanbirkner</groupId>
          <artifactId>system-rules</artifactId>
          <version>1.16.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.9</version>
        </dependency>
        <!--其他需要的包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
    
    

    3.在项目工程目录上右键选择 Add FrameWork Support

    4.png
    如果找不到下图中的Spring 选项请参考这里:https://blog.csdn.net/dainandainan1/article/details/80318780
    5.png
    选择完后他会自动帮你下载同时我们的工程目录下也多了几个文件:
    6.png
    1. 在main目录webapps 同级目录下创建java 文件夹


      7.png

      但是此时的java文件是灰色的,选中java文件:如下图所示,就变成蓝色了


      8.png
      4创建自己所需要的文件夹
      . 9.jpg

      5.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>
      <display-name>Archetype Created Web Application</display-name>
     
        <!-- 将applicationContext.xml添加进来 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
     
        <!--配置springmvc DispatcherServlet-->
        <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <!--配置dispatcher.xml作为mvc的配置文件-->
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
     
        <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>*.form</url-pattern>
        </servlet-mapping>
    </web-app>
    
    

    6.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">
     
        <!--启用spring的一些annotation -->
        <context:annotation-config/>
     
        <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
        <mvc:annotation-driven/>
     
        <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
        <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
        <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
            <property name="suffix" value=".jsp"/>
            <property name="exposeContextBeansAsAttributes" value="true"/>
        </bean>
     
        <!-- 自动扫描装配 -->
        <context:component-scan base-package="com.flyz.controller"/>
    </beans>
    
    

    这句 <context:component-scan base-package="com.flyz.controller"/> 中 base-package一定要定位到 controller 目录否则,访问不到控制器:


    1552013350(1).jpg
    1. controller包 和jsp包下分别创建 IndexController.java 和 success.jsp
      IndexController :


      16.png
    package com.flyz.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class IndexController {
        @RequestMapping("/index")
        public String index() {
            return "sucess";
        }
    }
    
    

    success.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: wscjw
      Date: 2019-03-08
      Time: 9:20
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
      <body>
      Welcome to SpringMVC World!!!
      </body>
    </html>
    
    

    7.Tomcat部署(前提是,IDEA Tomcat已经配置好了)


    11.png
    12.jpg

    8.点击OK之后发现Tomcat 上面打了红色叉


    13.png
    还点击它Edit -Configurations:
    14.jpg 15.png

    点击运行项目即可


    LUG7Q@SQ~RL`2]{976`V936.png

    相关文章

      网友评论

          本文标题:【一】使用IDEA Maven 搭建SpringMVC环境

          本文链接:https://www.haomeiwen.com/subject/svslpqtx.html