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/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/context
              http://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 {
      
          @Autowired
          private AccountService accountService;
      
          /**
           * 查询所有方法
           */
          @RequestMapping("/findAll")
          public ModelAndView findAll(){
              System.out.println("表现层:查询所有账户");
              List<Account> list = accountService.findAll();
              ModelAndView mv = new ModelAndView();
              mv.setViewName("suc");
              return mv;
          }
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值