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

Spring框架从入门到入土(七):Spring和Web

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

Spring框架从入门到入土(七):Spring和Web

Spring框架从入门到入土(七):Spring和Web

在web项目中使用spring框架,首先要解决在web层(这里指的是servlet)中获取spring容器的问题,只要在web层获取到了spring容器,便可以获取到service对象。

搭建Web项目环境
在web项目中使用spring,完成学生注册

实现步骤:
1. 创建maven项目,web项目
2. 加入依赖 jsp依赖和servlet依赖
3. 编写代码和配置文件
4. 创建jsp发起请求,有参数id,name,email,age
5. 创建Servlet,接收请求,调用Service和Dao完成注册
6. 创建一个jsp作为显示页面

部分代码如下:

  • 创建maven项目

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


    注册学生页面


注册学生页面
id:
姓名:
邮件:
年龄:

  • servlet类
package com.liar.controller;

import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String age = req.getParameter("age");
        System.out.println(name);

        //创建spring容器对象
        String config = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(config);
        System.out.println("容器的对象信息"+context);

        //获取service
        StudentService studentService = (StudentService) context.getBean("studentService");
        Student student = new Student();
        student.setId(Integer.parseInt(id));
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setEmail(email);
        studentService.addStudent(student);

        //给一个页面
        req.getRequestDispatcher("/result.jsp").forward(req,resp);
    }
}
用监听器改进

需求:web对象只需要创建一次,把容器对象放入到全局作用域servletContext中

实现:使用监听器,当全局作用域被创建的时,创建容器,存入servletContext

监听器作用:可以使用框架中提供的ContextLoaderListener

  • 创建容器对象
  • 把容器对象存入到servletContext

实现步骤:

  1. 加入依赖
    
    
      org.springframework
      spring-web
      5.2.5.RELEASE
    
  1. web.xml注册监听器
   
    
        
        contextConfigLocation
        
        classpath:springContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
  1. 修改servlet
package com.liar.controller;

import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String age = req.getParameter("age");

        WebApplicationContext context = null;
        //获取ServletContext对象中的方法,创建好的容器对象,拿来就用
        String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
        Object attr = getServletContext().getAttribute(key);
        if(attr != null){
            context = (WebApplicationContext)attr;
        }
        System.out.println("容器的对象信息"+context);

        //获取service
        StudentService studentService = (StudentService) context.getBean("studentService");
        Student student = new Student();
        student.setId(Integer.parseInt(id));
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setEmail(email);
        studentService.addStudent(student);

        //给一个页面
        req.getRequestDispatcher("/result.jsp").forward(req,resp);
    }
}
工具类使用
package com.liar.controller;

import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String age = req.getParameter("age");

        WebApplicationContext context = null;
        ServletContext sc = getServletContext();
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器的对象信息"+context);

        //获取service
        StudentService studentService = (StudentService) context.getBean("studentService");
        Student student = new Student();
        student.setId(Integer.parseInt(id));
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setEmail(email);
        studentService.addStudent(student);

        //给一个页面
        req.getRequestDispatcher("/result.jsp").forward(req,resp);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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