加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

一起学Spring之Web基础篇

发布时间:2020-12-15 01:17:14 所属栏目:大数据 来源:网络整理
导读:概述 在日常的开发中Web项目集成Spring框架,已经越来越重要,而Spring框架已经成为web开发的主流框架之一。本文主要讲解Java开发Web项目集成Spring框架的简单使用,以及使用Spring和不使用Spring框架,两者之间的差异。 仅供学习分享使用,如有不足之处,还

概述

在日常的开发中Web项目集成Spring框架,已经越来越重要,而Spring框架已经成为web开发的主流框架之一。本文主要讲解Java开发Web项目集成Spring框架的简单使用,以及使用Spring和不使用Spring框架,两者之间的差异。 仅供学习分享使用,如有不足之处,还请指正。

页面访问流程图

本示例的页面访问流程图如下所示:

?

不使用Spring框架的开发流程

步骤如下:

1. 新增Service和Dao对应的类及接口实现

如下所示:在Service中对Dao进行了强关联

 1 package com.hex.Dao;
 2 
 3 /**
 4  * 学生Dao
 5  * @author Administrator
 6  *
 7  */
 8 public interface IStudentDao {
 9     public String GetStudentById(int id);
10 }
11 ////////////////////////////////////////
12 13 
14 15 16 17 18  19 class StudentDaoImpl implements20 
21     22      * 查询学生信息
23      24     @Override
25      id) {
26         
27         return "hex";
28     }
29 
30 31 32  com.hex.Service;
33 
34 35  * 学生服务接口
36 37 38  39  IStudentService {
40     41 42 43 44 
45 import com.hex.Dao.IStudentDao;
46  com.hex.Dao.StudentDaoImpl;
47 
48 49  * 学生服务实现类
50 51 52  53 class StudentServiceImpl 54 
55     private IStudentDao studentDao;
56     
57     void setStudentDao(IStudentDao studentDao) {
58         this.studentDao = studentDao;
59 60 
61 62     63         //studentDao=new StudentDaoImpl();
64         return studentDao.GetStudentById(id);
65 66 
67 }
View Code

2. 新增HomeServlet类,并需要通过new的方式声明studentService对象

如下所示:

 com.hex.servlet;
 * 访问Servlet实现类
17  18 class HomeServlet extends HttpServlet {
19     private static final long serialVersionUID = 1L20     
 IStudentService studentService;
22       
23 
24     25      * 构造函数26      27     public HomeServlet() {
28        
29 30     
31          * 初始化时声明studentService对象
33      35     void init() throws ServletException {
36         studentService=new StudentServiceImpl();
38 
39     40      * Get方法
41      42     protected void doGet(HttpServletRequest request,HttpServletResponse response)  ServletException,IOException {
43         
44         String studentName=studentService.GetStudentById(0);
45         request.setAttribute("studentName",studentName);
46         request.getRequestDispatcher("/jsp/Home.jsp").forward(request,response);
47 48 
49          * Post方法,此处和Get方法同
51      52     void doPost(HttpServletRequest request,1)">53          TODO Auto-generated method stub
54         doGet(request,1)">55 56 
57 }

3. 前端页面进行访问即可

如下所示:

1 <a href="../HomeServlet">点击进入</a>

4. 缺点:

此处形成了强依赖,即HomeServlet需要StudentServiceImpl对象。且StudentServiceImpl需要StudentDao的支持。

采用Spring的方式进行访问

0. Spring框需要的Jar包

Spring框架支持web项目需要的Jar包共7个,如下所示:

//日志包
 2 commons-logging-1.1.1.jar
//spring核心包
spring-aop-4.0.6.RELEASE.jar
spring-beans-4.0.6.RELEASE.jar
spring-context-4.0.6.RELEASE.jar
 7 spring-core-4.0.6.RELEASE.jar
spring-expression-4.0.6.RELEASE.jar
 9 //web包
10 spring-web-4.0.6.RELEASE.jar

?

1. 需要在web.xml文件中配置Spring对应的监听器

如下所示:

applicationContext.xml 位于src目录,所以需要加上classpath,是Spring容器的配置文件

context-param>
2     param-name>contextConfigLocation3     param-value>classpath:applicationContext.xml
4     5 6 <!-- 配置spring-web.jar对应的监听器 ,Tomcat启动时,自动初始化IOC容器 -->
7 listener8     listener-class>org.springframework.web.context.ContextLoaderListener9 2. 配置Spring的IOC容器

如下所示:依赖引用对象属性采用ref方式,如果是值对象,则采用value方式。

<?xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd" 6       Dao依赖于数据库的底层操作,本示例不予深入  7      bean id="studentDao" class="com.hex.Dao.StudentDaoImpl"></bean 8       Service层依赖于StudentDao,采用set的方式注入  9      ="studentService"="com.hex.Service.StudentServiceImpl"10          property name ref="studentDao"property11      beans3. 在Servlet中,引入ApplicationContext对象,将Tomcat容器和Spring的IOC容器进行关联

如下所示:其他方法保持不变,增加studentService对象的getter和setter方法,然后通过容器注入的声明方式产生对象。在StudentServiceImpl中对StudengDao的依赖采用同样方法进行注入。

 * Servlet实现类
 5   7      8     
10        
11      IStudentService getStudentService() {
12          studentService;
13 14 
15      setStudentService(IStudentService studentService) {
16         this.studentService =18     
20      * 初始化时获取Sping的IOC容器中的bean对象
21      23     24         Web项目获取Spring上下文对象。
25         ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
26         studentService=(IStudentService)context.getBean("studentService"27 28 }

4. 优势:

此方式将Servlet和Service及Dao之间进行了解耦,灵活扩展性大大增强。

小知识

如果Spring的IOC容器文件有多个,可以采用Import的方式进行引入,如下所示:

 第二种方式,采用import方式引入其他容器文件 2 import resource="applicationContext2.xml"/>

在web.xml中配置servlet的方式,如下所示:

web-app ="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" 3     display-name>FirstWebSpring 4     servlet 5         description 6          7         >HomeServlet 8         servlet-name 9         servlet-class>com.hex.servlet.HomeServlet10     servlet-mapping13         url-pattern>/HomeServlet14     welcome-file-listwelcome-file>index.html17         >index.htm18         >index.jsp19         >default.html20         >default.htm21         >default.jsp22      配置容器地址  第一种方式如果要加载多个配置文件,可以写多个,如下所示:
        <param-value>
26             classpath:applicationContext.xml,            classpath:applicationContext2.xml
        </param-value>
29      30     31         32         33             classpath:applicationContext.xml
34         36     37     38         web-app>
View Code

备注

绳锯木断,水滴石穿。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读