【Spring】Spring和Web(最后一章)

【Spring】Spring和Web(最后一章),第1张

【Spring】Spring和Web(最后一章) Spring学习接近尾声~~~~~~~~~


spring-web完成学生注册功能。

步骤:

  1. 新建 maven
  2. 修改pom.xml:spring、mybatis以外的依赖、 servlet 、 jsp依赖
  3. 创建实体类 Student, 对应Student2表
  4. 创建dao接口和mapper文件
  5. 创建mybatis主配置文件
  6. 创建service和实现类
  7. 创建servlet,接收请求的参数,调用service对象
  8. 创建jsp,提交请求参数
  9. 创建jsp,作为视图,显示请求的处理结果
  10. 创建spring配置文件

1.创建一个maven - web项目

2.添加依赖

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
    
      javax.servlet.jsp
      jsp-api
      2.2.1-b03
      provided
    


    
      junit
      junit
      4.11
      test
    

    
    
      org.springframework
      spring-web
      5.2.5.RELEASE
    

    
    
      org.springframework
      spring-context
      5.2.5.RELEASE
    

    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    

    
    
      org.mybatis
      mybatis
      3.5.1
    

    
    
      org.mybatis
      mybatis-spring
      1.3.1
    

    
    
      mysql
      mysql-connector-java
      5.1.9
    

    
    
      com.alibaba
      druid
      1.1.12
    

插件:


    
      
        src/main/java
        
          ***.xml
        
        false
      
    
  

3.目录结构:创建java、resources并标记

4.创建实体类和接口类

5.创建mapper文件


6.主配置文件:Spring、mybatis-config、propertices

7.创建业务层service和实现类
业务层方法:动词+名词(见名知意)



8.创建servlet,接收请求的参数,调用service对象





9.创建jsp,提交请求参数
将原来的index.jsp删掉,用idea模板重新生成一个jsp文件

配置服务器:





数据库中有记录


查询学生:
表单

新建一个servlet:

	
        QueryStudentServlet
        com.bjpowernode.controller.QueryStudentServlet
    
    
        QueryStudentServlet
        /queryStudent
    


在QueryStudentServlet的doGet方法中:


启动服务器,在浏览器输入地址:



找了半天不知道哪里出错了。。。。。。查询结果不显示但是在控制台有输出对象信息。

1 现在使用容器对象的问题

1.创建容器对象次数多
2.在多个servlet中,分别创建容器对象

2 需要一个什么样的容器对象

1.容器对象只有一个, 创建一次就可以了
2.容器对象应该在整个项目中共享使用。 多个servlet都能使用同一个容器对象

解决问题使用监听器 ServletContextListener (两个方法 初始时执行的,销毁时执行的)
在监听器中,创建好的容器对象,应该放在web应用中的ServletContext作用域中。

3 ContextLoaderListener 监听器对象

ContextLoaderListener 是一个监听器对象, 是spring框架提供的, 使用这个监听器作用:
1.创建容器对象:一次
2.把容器对象放入到ServletContext作用域。

使用步骤:
1.pom.xml加入依赖spring-web


  org.springframework
  spring-web
  5.2.5.RELEASE

2.web.xml 声明监听器


默认监听器:创建容器对象时,读取的配置文件: /WEB-INF/applicationContext.xml
自定义容器使用的配置文件路径:
context-param:叫做上下文参数, 给监听器,提供参数的

监听器的设置:

启动服务器:成功

4 ContextLoaderListener 源代码
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    //监听器的初始方法
    public void contextInitialized(ServletContextEvent event) {
        this.initWebApplicationContext(event.getServletContext());
    }

}


初始化方法内部:核心部分

查看context:

private WebApplicationContext context;
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
            try {
                if (this.context == null) {
                    //创建srping的容器对象
                    this.context = this.createWebApplicationContext(servletContext);
                }
                
           //把容器对象放入的ServletContext作用域
          //key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
          //value=容器对象
                servletContext.setAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,                             this.context);
               
            } catch (Error | RuntimeException var8) {
     
            }
        }
    }

 

WebApplicationContext是web项目中使用的容器对象

public interface WebApplicationContext extends ApplicationContext 


通过以上方法创建容器对象:无论发多少次请求,对象只创建了一个并且共享。

把mybatis文件中的日志去掉就清爽很多了

请求查询多次:

//使用Spring提供的工具方法,获取容器对象
ServletContext sc = getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

//使用Spring提供的工具方法,获取容器对象
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5438160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存