在Maven上Web项目添加Spring框架

news/2024/7/7 13:23:51

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.  编写范例,运行

 

转载于:https://www.cnblogs.com/knsbyoo/p/9207225.html


http://www.niftyadmin.cn/n/2752949.html

相关文章

Ubuntu下 DevToolsActivePort file doesn't exist 错误

错误描述 Ubuntu下运行一个selenium调用无头chrome浏览器进行爬取的Python程序报出如下的错误&#xff1a; (unknown error: DevToolsActivePort file doesnt exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDri…

LOW逼三人组(一)----冒泡算法

排序 1、冒泡排序 冒泡算法 import random # 随机模块 def bubble_sort(li): ###################################冒泡排序#####################################for i in range(len(li)-1): # 多少趟for j in range(len(li)-i-1): #一趟里多少次if li[j]>li…

浏览器输入URL回车会发生什么

这是一个非常经典的面试题,这道题没有具体的答案,它涉及很多的知识点&#xff0c;面试官会通过这道题了解你对哪一方面的知识比较擅长&#xff0c; 然后继续追问看看你的掌握程度。当然我写的这些也只是我的一些简单的理解&#xff0c;从前端的角度出发&#xff0c;我觉得首先回…

Redis:WRONGTYPE Operation against a key holding the wrong kind of value

错误信息 redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value分析 当前程序中key的操作类型&#xff0c;并不与redis库中存在的key的类型相匹配。 举例&#xff1a; 第一次保存key&#xff0c;将其设…

epoll_wait会被系统中断唤醒

今天&#xff0c;当一个程序在epoll_wait阻塞时&#xff0c;用strace跟踪了一下&#xff0c;结果epoll_wait就被EINTR唤醒了&#xff0c;并且返回-1&#xff1b; 所以&#xff0c;当epoll_wait返回-1时&#xff0c;需要判断errno是不是EINTR&#xff0c;如果是&#xff0c;继续…

iOS开发常用代码块

遍历可变数组的同时删除数组元素 NSMutableArray *copyArray [NSMutableArray arrayWithArray:array]; NSString *str1 “zhangsan”; for (AddressPerson *perName in copyArray) { if ([[perName name] isEqualToString:str1]) { [array removeObject:perName]; } …

Redis 启动警告解决

警告 Increased maximum number of open files to 10032 (it was originally set to 1024).问题分析 这条警告是说&#xff1a;最大文件打开数不够。 解决方法一 如果是root用户&#xff0c;那么修改下 ulimit 就可以。 #查看系统限制 ulimit -a#设置“open files”数量 u…

JS事件类型有哪些?分别是什么?

JavaScript可以处理的事件类型为:鼠标类型、键盘类型和HTML类型! 注意:所有的事件处理函数都由两个部分组成, on事件名称。 例如:click事件,处理函数就是onclick 现在我们来看第一种 鼠标事件 click:单击鼠标按钮时触发 dblclick:当用户双击鼠标按钮时触发 mousedown :当…