当前位置: 首页 > news >正文

SpringMVC基础

简介

Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面;Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块;使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用 Spring的SpringMVC 框架或集成其他MVC开发框架;下面将演示搭建第一个 SpringMVC 项目

实现步骤

  1. 首先我们先创建一个动态 web 项目,名为:SpringMVC;如果不用 maven 的话,Spring 的对应 jar 包可以直接在这个网址下载:https://repo.spring.io/libs-release-local/org/springframework/spring/

  2. 将 Spring 的包全部导入到项目的 lib 文件夹下,除了 Spring 的包之外,还有一个 commons-logging.jar 包一样要导入进去

  3. 在 web.xml 中添加如下内容;注意这里的 servlet-class:org.springframework.web.servlet.DispatcherServlet,我们要使用 Spring 的DispatcherServlet 来控制流程,拦截项目中其他的 xml 文件

<servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern>
</servlet-mapping>

还可以顺便加上中文过滤器

<!-- 字符过滤器 -->
<filter><filter-name>CharacterFilter</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>
</filter>
<filter-mapping><filter-name>CharacterFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
  1. 在 web.xml 同目录下创建一个 spring-servlet.xml 文件

  2. 给 spring-servlet.xml 文件添加相应的 schema 配置, 可以通过打开 \docs\spring-framework-reference\htmlsingle.html 文件,然后搜索:‘xmlns:mvc’ 找到相应的 schema,注意还要添加 context 的 schema,最基本的内容如下

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  1. 配置如下内容,context:component-scan是指程序会在哪些包下面去找添加了 annotation 的类 mvc:annotation-driven/ 是指打开 SpringMVC的annotation功能;最后的 bean,InternalResourceViewResolver 是指我们选择这种方式来映射 view,里面的两个配置分别是返回映射的前缀和后缀,假如:在controller 中返回了 ‘hello’ 字符串,那么,view的路径就是 view path = prefix + ‘hello’ + ‘.jsp’
<context:component-scan base-package="com.ibm.reskill"/>
<mvc:annotation-driven/>
<!--(推荐)第一种:视图层配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><! -- 可省略 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/>
</bean><!--第二种:视图层配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"><! -- 可省略 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" />
</bean>

7.新建一个controller.class来测试

@
Controller("testController")
@ Scope("singleton") //单例模式,默认,可省略;多例模式的话,应配置成 prototype
public class TestController {@RequestMapping({"/hello", "/"})public String hello() {System.out.println("hello");return "hello";}
}

注意:如果按照以上步骤操作,出现404错误,并发现 nohandlerfound 异常

(1) 仔细检查每一个配置文件中的配置内容是否正确

(2) 如果确定每一个配置文件正确,引用的class也没有问题,那么可以尝试在 eclipse 中手动 bulid project

http://www.lryc.cn/news/6175.html

相关文章:

  • 低代码开发平台|制造管理-质检管理搭建指南
  • 推荐一个.Ner Core开发的配置中心开源项目
  • Vue3+vite4使用mockjs进行模拟开发遇到的坑
  • 一起Talk Android吧(第四百九十三回:动画知识总结)
  • 腾讯云企业网盘正式入驻数字工具箱
  • 2.13练习
  • 【iOS】APP IM聊天框架的设计(基于第三方SDK)
  • centos安装FastDFS,集成到SpringBoot中
  • 看透react源码之感受react的进化
  • 【最优化理论】线性规划
  • 数据库测试的认知和分类
  • MQ中间件概念一览
  • 爱尔兰公司注册要求及条件
  • Java中如何打印对象内存地址?
  • CF1707E Replace
  • 【Hello Linux】Linux工具介绍 (make/makefile git)
  • 享元模式flyweight
  • Pulsar
  • 项目介绍 + 定长内存池设计及实现
  • Linux--线程安全的单例模式--自旋锁--0211
  • 图文解说S参数(进阶篇)
  • Sentinel源码阅读
  • 2023年浙江食品安全管理员考试真题题库及答案
  • Webstorm 代码没有提示,uniapp 标签报错
  • MySQL-Innodb引擎事务原理
  • Linux操作系统学习(了解环境变量)
  • 数据分析思维(六)|循环/闭环思维
  • C++:类和对象(下)
  • ASP.NET Core MVC 项目 AOP之IResultFilter和IAsyncResultFilter
  • jstack排查cpu占用高[复习]