• 使用Maven搭建SpringMVC


    1.创建Maven Project

    注意选择webapp

    2.添加Maven依赖

    <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>springmvctest</groupId>
        <artifactId>com.test</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>com.test 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 -->
            <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 -->
        </dependencies>
        <build>
            <finalName>com.test</finalName>
        </build>
    </project>
    View Code

    注意:1.jar包下不下来解决方案:找到Maven本地仓库,删除对应文件重新下载,如果还不能下载,到网上下载jar包放在对应目录。

               2.${spring.version}对应的版本<spring.version>3.1.2.RELEASE</spring.version>

    3.设置Properties

    3.1.Project Facets把java改为1.7

    3.2.Java Build Path 更改JRE System Library为

    3.3.Java Build Path 点击Add Library;点击Server Runtime;然后...

    4.配置web.xml文件

    <web-app version="3.0" 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_3_0.xsd">
        
      <display-name>Archetype Created Web Application</display-name>
      
      <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!--1.如果使用下面注释配置请将名为springContext.xml配置文件放在src/main/resources下
              2.如果注释请用dispatcher-servlet.xml放在WEB-INF下,程序默认搜索WEB-INF名为xxx-servlet.xml
                xxx为自己定义的<servlet-name>dispatcher</servlet-name>的dispatcher
            -->
             <!-- <init-param>
                 <param-name>contextConfigLocation</param-name>
              <param-value>classpath*:springContext.xml</param-value> 
             </init-param> -->
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    View Code

    注意:使用classpath*把配置文件放在webapp下或者WEB-INF下程序运行不会出错(一般找不到文件),但是Controller会拦截不到。

    5.配置dispatcher-servlet.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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">
        
        <!-- 搜索Controller包 -->
        <context:component-scan base-package="com.test"></context:component-scan>
        <!-- 视图页面配置 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/views/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    </beans>
    View Code

    6.编写Controller代码和jsp

    省略

    7.配置tomcat及运行

  • 相关阅读:
    SpringMVC—概述
    Spring—切点表达式
    Spring—Ioc
    Spring—spring概述
    MyBtis—原理及初始化
    mysql数据增删查授权
    mysql完整性约束
    mysql枚举类型与集合类型
    mysql字符类型
    mysql日期类型
  • 原文地址:https://www.cnblogs.com/SmilingEye/p/7491754.html
Copyright © 2020-2023  润新知