SSH笔记-web应用下使用Spring

1、在web应用下使用Spring的思路:
(1)额外需要加入的spring jar包:
①spring-web-x.x.x.RELEASE.jar
②spring-webmvc-x.x.x.RELEASE.jar

(2)创建IOC容器
①非WEB:在main方法中创建

(3)访问IOC容器
①在容器创建后,可以放到ServletContext(即:application域)的属性中

(4)开发步骤:
一般直接使用方法二,如果需要特别指定逻辑,则使用方法一

方法一:通过配置listener类和调用HttpServlet类,实现web中使用Spring
        (1)创建对应的bean
        (2)创建Spring配置文件(applicationContext.xml),并配置bean
        (3)创建一个实现ServletContextListener接口的类,并且创建IOC容器,把容器放到ServletContext属性中
        (4)在WEB-INF下的web.xml中配置listener和配置Spring配置文件
        (5)创建一个继承HttpServlet的类,实现doGet或doPost方法
        (6)在上一步的doGet或doPost方法中,从application域对象获取IOC容器的引用,并且从IOC容器中获取需要的bean
        (7)写一个页面通过调用action的操作来调用第五步的类的doGet或doPost方法

方法二:通过配置ContextLoaderListener,实现web中使用Spring
        (1)创建对应的bean
        (2)创建Spring配置文件(applicationContext.xml),并配置bean
        (3)在WEB-INF下的web.xml中配置ContextLoaderListener的listener和配置Spring配置文件
        (4)jsp页面上通过WebApplicationContextUtils从application域对象中获得IOC容器的实例,并且获取和使用bean实例

2、文件
(1)通过配置listener类和调用HttpServlet类,实现web中使用Spring
①Product.java:数据模型
②SpringServletContextListener.java:实现ServletContextListener接口的类
③TestServlet.java:用于创建IOC容器,把容器放到ServletContext属性中
④applicationContext.xml:Spring配置文件
⑤web.xml:在这里配置listener和配置Spring配置文件
⑥index.jsp:测试页面

(2)通过配置ContextLoaderListener,实现web中使用Spring
①Product.java:数据模型
②applicationContext.xml:Spring配置文件
③web.xml:在这里配置listener和配置Spring配置文件
④index.jsp:测试页面

3、Product.java

package com.demo.sshtest.bean;

public class Product {

    private Integer id;
    private String name;

    public Product() {
        super();
        System.out.println("ProductProductProduct");
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void PrintProduct(){
        System.out.println("ID:"+id+"----NAME:"+name);
    }
}

4、SpringServletContextListener.java

package com.demo.sshtest.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebListener
public class SpringServletContextListener implements ServletContextListener {

    public SpringServletContextListener() {
    }

    public void contextDestroyed(ServletContextEvent arg0)  { 
    }

    public void contextInitialized(ServletContextEvent arg0)  {
        //0、获取Spring配置文件名称
        ServletContext servletContext = arg0.getServletContext();
        String config = servletContext.getInitParameter("contextConfigLocation");
        //1、创建IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
        //2、把IOC容器放到ServletContext属性中
        servletContext.setAttribute("ApplicationContext", applicationContext);
    }

}

5、TestServlet.java

package com.demo.sshtest.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;

import com.demo.sshtest.bean.Product;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public TestServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、从application域对象获取IOC容器的引用
        ServletContext servletContext = getServletContext();
        ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("ApplicationContext");

        //2、从IOC容器中获取需要的bean
        Product product = applicationContext.getBean(Product.class);
        product.PrintProduct();
    }

}

6、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <bean id="product" class="com.demo.sshtest.bean.Product">
        <property name="id" value="1"></property>
        <property name="name" value="name1"></property>
    </bean>

</beans>

7、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

8、index.jsp

<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="com.demo.sshtest.bean.Product"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--

在web应用下使用Spring的思路:
1、额外需要加入的spring jar包:
    (1)spring-web-x.x.x.RELEASE.jar
    (2)spring-webmvc-x.x.x.RELEASE.jar
2、创建IOC容器
    (1)非WEB : 在main方法中创建
    (2)WEB  : 在web应用被服务器加载时创建,即:ServletContextListener.contextInitialized(ServletContextEvent sce)中创建
3、访问IOC容器
    (1)在容器创建后,可以放到ServletContext(即:application域)的属性中
4、开发步骤:
    一般直接使用方法二,如果需要特别指定逻辑,则使用方法一

    方法一:通过配置listener类和调用HttpServlet类,实现web中使用Spring
        (1)创建对应的bean
        (2)创建Spring配置文件(applicationContext.xml),并配置bean
        (3)创建一个实现ServletContextListener接口的类,并且创建IOC容器,把容器放到ServletContext属性中
        (4)在WEB-INF下的web.xml中配置listener和配置Spring配置文件
        (5)创建一个继承HttpServlet的类,实现doGet或doPost方法
        (6)在上一步的doGet或doPost方法中,从application域对象获取IOC容器的引用,并且从IOC容器中获取需要的bean
        (7)写一个页面通过调用action的操作来调用第五步的类的doGet或doPost方法

    方法二:通过配置ContextLoaderListener,实现web中使用Spring
        (1)创建对应的bean
        (2)创建Spring配置文件(applicationContext.xml),并配置bean
        (3)在WEB-INF下的web.xml中配置ContextLoaderListener的listener和配置Spring配置文件
        (4)jsp页面上通过WebApplicationContextUtils从application域对象中获得IOC容器的实例,并且获取和使用bean实例

-->


    <!-- 通过HttpServlet,在web页面上使用Spring
    <a href="TestServlet">TestServlet</a>
    -->
    <!-- 通过获取application域,在web页面上使用Spring -->
    <%
        //1、从application域对象中获得IOC容器的实例
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);
        //2、从IOC容器中得到bean
        Product product = applicationContext.getBean(Product.class);
        //3、使用bean
        product.PrintProduct();
    %>
</body>
</html>

9、注意事项
①相对来讲,通过配置ContextLoaderListener,实现web中使用Spring更加方便
②不管用那种配置方法,都需要到web.xml中配置listener和配置Spring配置文件
③jar包版本要统一,不然会出现加载不了的报错或者以下不能运行的报错

10、项目目录
项目目录

11、demo
https://download.csdn.net/download/qq_22778717/10484634

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值