• 在Maven上Web项目添加Spring框架


    1. pom.xml添加Spring依赖包

        <!-- spring 核心依赖-->
        <!-- context依赖beans,aop,core,expression;core依赖logging;所以一次导入6个包-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!-- webmvc依赖web;导入web和webmvc包-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!-- spring 核心依赖-->
    

      查看项目的依赖,可以发现依赖层级关系,导入了其他的架包。

    2.  添加servlet,jsp架包

       <!-- servlet,jsp依赖-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
        </dependency>
        <!-- servlet,jsp依赖-->
    

      查看项目依赖:

    3.  在web.xml配置Spring MVC DispatcherServlet,配置Spring过滤器

     <!-- Spring MVC DispatcherServlet-->
      <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!-- encodingFilter -->
      <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>
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/</url-pattern>
      </filter-mapping>
    

    4. 配置Spring contextConfigLocation,这里是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: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/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 配置包扫描器 -->
        <context:component-scan base-package="self.springmvc.*"/>
        <!-- 配置注解驱动 -->
        <mvc:annotation-driven/>
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    5.  编写范例,运行

  • 相关阅读:
    Sails v1.0使用指南
    sails0.12相关命令
    vs code 快捷键
    nodejs相关框架
    sails 相关软件下载地址及命令
    UltraISO安装windows10时0x8007000D错误解决办法
    C# HtmlElement的GetAttribute("class") return ""
    Android如何实现TCP和UDP传输
    android 获取view在屏幕中的位置
    八款Android 开发者必备的小工具
  • 原文地址:https://www.cnblogs.com/knsbyoo/p/9207225.html
Copyright © 2020-2023  润新知