SSM框架整合(4)—— Spring整合SpringMVC

Spring 整合 SpringMVC

结构目录

  • java
    • controller
      • AccountController.java(表现层)
    • dao
      • AccountDao.java(持久层)
    • domain
      • Account.java(JavaBean对象)
    • service
      • AccountService.java(业务层)
      • AccountServiceImp.java(业务层)
  • resources
    • applicationContext.xml(Spring配置文件)
    • springmvc.xml(SpringMVC配置文件)
  • webapp
    • WEB-INF
      • pages
        • list.jsp(jsp页面文件)
      • web.xml(web配置文件)
    • index.jsp(jsp页面文件)

web配置文件

  • 监听器
    • 配置 监听器(ContextLoaderListener)
    • 配置 服务器启动时,加载配置文件(contextConfigLocation)
  • 前端控制器
  • 过滤器

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 监听器 -->
    <!-- 服务器启动时,加载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>


  <!-- 前端控制器 -->
    <!-- 服务器启动时,DispatcherServlet对象被创建,加载SpringMVC配置文件 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <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>

  <!-- 过滤器 -->
  <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>

</web-app>

表现层

  • 通过依赖反转获取 业务层对象,并执行业务层的方法,到达Spring与SpringMVC的整合。

AccountController.java

package cn.water.controller;

import cn.water.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("accountController")
public class AccountController {

    @Autowired
    private AccountService service;

    @RequestMapping("findAll")
    public String findAll(){
        /* 表现层 */
        System.out.println("表现层:查询所有用户信息");
        /* 业务层 */
        service.findAll();
        return "list";
    }

}

执行结果

点击 index.jsp 页面,执行表现层中的findAll(),和业务层的findAll(),最后跳转到 list.jsp 页面。

表现层:查询所有用户信息
业务层:查询所有用户
发布了68 篇原创文章 · 获赞 2 · 访问量 1896

猜你喜欢

转载自blog.csdn.net/qq_40981851/article/details/104218739