在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" %>
注册学生页面
注册学生页面
- 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
实现步骤:
- 加入依赖
org.springframework
spring-web
5.2.5.RELEASE
- web.xml注册监听器
contextConfigLocation
classpath:springContext.xml
org.springframework.web.context.ContextLoaderListener
- 修改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);
}
}



