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

SSM框架探秘:Spring 整合 SpringMVC 框架

  1. 搭建和测试 SpringMVC 的开发环境:
    1. web.xml 元素顺序:

    2. 在 web.xml 中配置 DisPatcherServlet 前端控制器:
      <!-- 配置前端控制器 -->
      <servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 加载 springmvc.xml 配置文件 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 启动加载 --><load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern>
      </servlet-mapping>
    3. 在 web.xml 中配置 DispatcherServlet 过滤器解决中文乱码:
      <!-- 解决 post 请求中文乱码的过滤器 -->
      <filter><filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern>
      </filter-mapping>
    4. 创建 springmvc.xml 配置文件,编写配置文件:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 扫描 controller 的注解 --><context:component-scan base-package="com.qcby.controller"/><!-- 配置视图解析器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- JSP 文件所在的目录 --><property name="prefix" value="/WEB-INF/pages/"/><!-- 设置文件的后缀名 .jsp --><property name="suffix" value=".jsp"/></bean><!-- 设置静态资源不过滤 -->
      <!--    <mvc:resources mapping="/css/**" location="/css/"/>-->
      <!--    <mvc:resources mapping="/js/**" location="/js/"/>-->
      <!--    <mvc:resources mapping="/images/**" location="/images/"/>--><!-- 开启对 SpringMVC 注解的支持 --><mvc:annotation-driven/>
      </beans>
    5. 测试 SpringMVC 的框架搭建是否成功:
      1. 编写 index.jsp 和 suc.jsp 页面:
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <body><h2>Hello World!</h2><a href="/account/findAll">查询所有</a></body>
        </html>//----------- suc 页面 ------------
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head><title>Title</title>
        </head>
        <body>
        <h1>跳转成功</h1>
        </body>
        </html>
      2. 创建 AccountController 类,编写方法进行测试:
        @Controller
        @RequestMapping("/account")
        public class AccountController {/*** 查询所有方法*/@RequestMapping("/findAll")public ModelAndView findAll(){System.out.println("表现层:查询所有账户");ModelAndView mv = new ModelAndView();mv.setViewName("suc");return mv;}
        }
  2. Spring 整合 SpringMVC 框架:
    1. 目的:在 controller 中能成功地调用 service 对象中的方法
    2. 在项目启动的时候,就去加载 applicationContext.xml 的配置文件,在 web.xml 中配置 ContextLoaderListener 监听器(监听器只能加载 WEB-INF 目录下的 applicationContext.xml 的配置文件)
      <!-- 配置 Spring 的监听器 -->
      <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 配置加载路径的配置文件 -->
      <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value>
      </context-param>
    3. 在 controller 中注入 service 对象,调用 service 对象的方法进行测试:
      @Controller
      @RequestMapping("/account")
      public class AccountController {@Autowiredprivate AccountService accountService;/*** 查询所有方法*/@RequestMapping("/findAll")public ModelAndView findAll(){System.out.println("表现层:查询所有账户");List<Account> list = accountService.findAll();ModelAndView mv = new ModelAndView();mv.setViewName("suc");return mv;}
      }
http://www.lryc.cn/news/526838.html

相关文章:

  • 2025.1.20——二、buuctf BUU UPLOAD COURSE 1 1 文件上传
  • 【架构面试】三、高可用高性能架构设计
  • 11.渲染管线——光栅化阶段
  • 【数据分享】1929-2024年全球站点的逐月平均能见度(Shp\Excel\免费获取)
  • 二叉树的深度
  • MySQL命令及用法(精华版)
  • R语言学习笔记之高效数据操作
  • 将 OneLake 数据索引到 Elasticsearch - 第二部分
  • Linux——冯 • 诺依曼体系结构
  • Java进阶(一)
  • appium自动化环境搭建
  • Qt 5.14.2 学习记录 —— 이십 QFile和多线程
  • 積分方程與簡單的泛函分析7.希爾伯特-施密特定理
  • 使用vitepress搭建自己的博客项目
  • 开始步入达梦中级dba
  • 如何在docker中的mysql容器内执行命令与执行SQL文件
  • S4 HANA更改Tax base Amount的字段控制
  • Linux权限有关
  • 【github 使用相关】提交pr和commit message Conventional Commits 规范 代码提交的描述该写什么?
  • Docker—搭建Harbor和阿里云私有仓库
  • Maven的下载安装配置
  • Rust:高性能与安全并行的编程语言
  • matlab的cat()函数详解(OK)
  • 将个人微信中的时间改成标准的日期时间格式
  • centos9编译安装opensips 二【进阶篇-定制目录+模块】推荐
  • 初步搭建并使用Scrapy框架
  • 基于SpringBoot的软件产品展示销售系统
  • pycharm 运行远程环境问题 Error:Failed to prepare environment.
  • Redis vs. 其他数据库:深度解析,如何选择最适合的数据库?
  • HTB:Support[WriteUP]