栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC详解(三)SSM整合开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC详解(三)SSM整合开发

文章目录
  • 第三章 SSM整合
    • 1. SSM整合思路
    • 2.容器的创建
    • 3. SSM整合开发的步骤
      • 3.1.使用student2表进行增删改查。(id,name,age)
      • 3.2.创建maven web 项目
      • 3.3.修改pom.xml加入依赖
        • pom.xml
      • 3.4.写web.xml:声明容器对象
        • web.xml
      • 3.5.创建程序中的包,dao,service,controller,entity
      • 3.6.写spring,springmvc,mybatis配置文件
        • 6.1spring 配置文件 applicationContext.xml
        • 6.2springmvc 配置文件 dispatcherServlet.xml
        • 6.3mybatis配置文件 mybatis.xml
        • 6.4jdbc.properties
      • 3.7.写java代码,实体类,dao接口和mapper文件,service类,controller类,使用注解声明对象和赋值
        • 7.1实体类
        • 7.2dao接口
        • 7.3mapper文件
        • 7.4service类
        • 7.5controller**类**
      • 3.8.创建视图文件,各种jsp
        • 8.1index.jsp
        • 8.2result.jsp
        • 8.3addStudent.jsp
        • 8.4 listStudent.jsp (ajax根据查询到的数据展示列表)
    • 4. 相对路径

第三章 SSM整合

SpringMVC+Spring +MyBatis(Ibatis),所以有人叫做SSI整合。SSM整合是使用三个框架的优势功能。

1. SSM整合思路

​ 三个框架对应的三层架构的三层。SpringMVC是视图层,Spring是业务层,MyBatis是持久层。

​ SSM整合,需要把对象交给容器管理,让容器去创建项目中要使用的java对象。现在有两个容器。

第一个容器是Spring容器:Spring容器是管理service和dao等对象。是业务层对象的容器。

第二个是SpringMVC容器:管理控制器对象的。是视图层对象。

​ SSM整合就是把对象交给容器管理。两个容器共存,各自负责管理不同的对象。把对象声明到配置文件中,让两个容器创建对象。spring创建service,dao ;springmvc创建controller。

2.容器的创建

​ Spring容器创建:在web.xml声明了监听器ContextLoaderListenner,这个功能框架写好了。功能是创建spring的容器对象WebApplicationContext。在创建WebApplicationContext对象时,读取spring的配置文件,读取文件的时候,遇到bean标签或者注解,就能创建service,dao等对象,放到容器中。

​ SpringMVC容器:在web.xml声明了中央调度器DispatcherServlet的init()方法中,创建了容器对象WebApplicationContext,在创建WebApplication对象时,读取springmvc的配置文件,读取文件的时候,遇到@Controller注解,创建控制器Controller对象,放到容器中。

​ 内存中,创建对象。

WebApplicationContext spring = new WebAplicationContext();  // spring-map(service.dao)
WebApplicationContext springmvc = new WebAplicationContext();//springmvc-map(controller)

​ SpringMVC容器和Spring容器的关系:设计上SpringMVC容器对象是Spring容器的子容器。

​ Spring是父容器。SpringMVC子容器。相当于java中的继承关系。子容器能访问父容器中的对象,controller对象能访问父中的service对象。

3. SSM整合开发的步骤 3.1.使用student2表进行增删改查。(id,name,age) 3.2.创建maven web 项目 3.3.修改pom.xml加入依赖

加入依赖:spring,springmvc,mybatis,mybatis-spring,mysql驱动,druid,jackson

pom.xml



  4.0.0

  com.sunny
  ch06-SSM
  1.0.0
  war



  
    UTF-8
    1.8
    1.8
  

  
    
      junit
      junit
      4.11
      test
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
    
      javax.servlet.jsp
      jsp-api
      2.2.1-b03
      provided
    
    
      org.springframework
      spring-webmvc
      5.2.5.RELEASE
    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.0
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
      mysql
      mysql-connector-java
      5.1.9
    

    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.0
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
      mysql
      mysql-connector-java
      5.1.9
    
    
      com.alibaba
      druid
      1.1.12
    

  

  
    ch06-SSM
    
      
        src/main/java
          
          
            ***.xml
          
          false
      
    
    
      
        maven-compiler-plugin
        3.1
        
          1.8
          1.8
        
      
    




3.4.写web.xml:声明容器对象

1)声明spring的监听器ContextLoaderListener:创建spring的容器对象,创建service,dao对象。

2)声明springmvc的中央调度器DispatherServlet:创建springmvc容器对象,创建controller对象。

3)声明字符集的过滤器CharacterEncodingFilter,解决post请求乱码的问题

web.xml



  
    contextConfigLocation
    classpath:conf/applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:conf/dispatcherServlet.xml
    
    1
  
  
    dispatcherServlet
    *.do
  


  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceRequestEncoding
      true
    
    
      forceResponseEncoding
      true
    
  
  
    characterEncodingFilter
    

@Service
public class StudentServiceImpl implements StudentService {

    
    @Autowired
    private StudentDao dao;

    @Override
    public int addStudent(Student student) {
        int rows = dao.insertStudent(student);
        return rows;
    }

    @Override
    public List queryStudents() {
        List list =dao.selectStudents();
        return list;
    }
}

7.5controller类
package com.sunny.controller;

import com.sunny.domain.Student;
import com.sunny.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;



@Controller
@RequestMapping("/student")
public class StudentController {

    
    @Autowired
    private StudentService service;

//    添加学生
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student){
        ModelAndView mv = new ModelAndView();

//        调用service,处理业务逻辑,把处理结果返回给用户
        int rows = service.addStudent(student);
        String msg = "注册失败!";
        if(rows>0){
            //注册成功,给用户成功的数据和视图
            msg = "注册成功!!";
        }
        mv.addObject("msg",student.getName()+","+msg);
        mv.setViewName("result");

        return mv;
    }
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List queryStudents(){
        List students=service.queryStudents();
        return  students;

    }
}

3.8.创建视图文件,各种jsp 8.1index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    浏览学生
    
    





    

浏览学生

id 姓名 年龄
8.2result.jsp
<%--
  Created by IntelliJ IDEA.
  User: 高昱泽
  Date: 2021/11/9
  Time: 21:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    result.jsp   , ${msg}



8.3addStudent.jsp
<%--
  Created by IntelliJ IDEA.
  User: 高昱泽
  Date: 2021/11/9
  Time: 21:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    添加


    

注册学生

姓名:
年龄:
操作:
8.4 listStudent.jsp (ajax根据查询到的数据展示列表)

<%--
  Created by IntelliJ IDEA.
  User: 高昱泽
  Date: 2021/11/9
  Time: 22:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    浏览学生
    
    





    

浏览学生

id 姓名 年龄
4. 相对路径

​ 在页面中,有路径的问题,访问路径有“/”开头的,还没有“/”。

没有斜杠开头
有斜杠开头

协议的地址

​ 地址的区别,现在看的都是页面中的地址。

1)有协议开头的例如http://www.baidu.com。称为绝对地址,地址是唯一的,能够直接访问。

2)没有协议开头的,例如test/some.do,/test/some.do称为相对地址。相对地址单独使用不能表示某个资源,不能访问。相对地址必须有参考地址在一起,才能表示一个资源的完整地址,才能访问。

参考地址:有“/”和没有“/”参考地址不同的。

1)没有斜杠开头的地址,参考地址:当前资源的访问路径。

当前访问的地址:http://localhost:8080/ch07_path/index.jsp

​	资源名称:index.jsp

​	资源路径:http://localhost:8080/ch07_path

在index.jsp 有访问地址< a href=“test/some.do”>

点击some.do后,地址变成 http://localhost:8080/ch07_path/test/some.do

当前访问的地址: http://localhost:8080/ch07_path/test/some.do

​	资源名称:some.do

​	资源路径:http://localhost:8080/ch07_path/test/

再去点击 test/some.do 地址: http://localhost:8080/ch07_path/test/test/some.do

没有斜杠开头的地址: 参考地址+当前的相对地址 组合在一起是最后的访问地址。

解决方式:

1)使用${pageContext.request.contextPath}。表示访问项目的路径(上下文 context path)
发起请求test/some.do

优点:好理解,
缺点:每个链接地址,都需要加el表达式

2)固定当前页面中的 没有“/”开头地址的参考地址。
使用html中base标签

<%
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + request.getContextPath() + "/";
%>

    请求方式
    
<%--    --%>
    
    

2)有斜杠开头的地址

< a href="/test/some.do">

访问的地址:http://localhost:8080/ch07_path/index.jsp
在index.jsp有相对地址 /test/some.do
点击链接后地址是:http://localhost:8080/test/some.do

使用“/”开头的地址,参考地址是服务器地址,也就是从协议到端口号位置 http://localhost:8080/
使用参考地址 http://localhost:8080/+相对地址 /test/some.do 
最后就是协议开始到端口号的位置
http://localhost:8080
地址缺少项目访问路径,ch07_path。

解决问题的方式:在你的路径前面加入el表示${pageContext.request.contextPath}

    

有斜杠开头的地址

/test/some.do
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459842.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号