目录
第六章、Spring与Web
6.1 创建Web项目
6.2 在pom.xml文件中加入依赖
6.3 创建实体类
6.3 创建Dao和mapper文件
6.4 创建Service及其实现类
6.5 MyBatis配置文件
6.6 Spring配置文件
6.7 前端jsp文件
6.8 创建Servlet
6.9 web.xml文件
6.10 总结
第六章、Spring与Web
之前做的javase项目中是有main方法或者测试方法的,执行代码是执行main方法的,因此在main方法里面创建容器对象。执行一次main方法,创建一次容器对象。
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
而web项目是在Tomcat服务器上运行的,tomcat一启动,项目就一直运行。
下面举一个实例:在web项目中使用Spring,完成学生注册功能。
6.1 创建Web项目
选择maven-archetype-webapp。
6.2 在pom.xml文件中加入依赖
为了使用监听器对象,记得加入相对应的依赖。
javax.servlet javax.servlet-api3.1.0 provided javax.servlet.jsp jsp-api2.2.1-b03 provided org.springframework spring-web5.2.5.RELEASE org.springframework spring-context5.2.5.RELEASE org.springframework spring-tx5.2.5.RELEASE org.springframework spring-jdbc5.2.5.RELEASE org.mybatis mybatis3.5.1 org.mybatis mybatis-spring1.3.1 mysql mysql-connector-java5.1.9 com.alibaba druid1.1.12
6.3 创建实体类
package com.bjpowernode.domain;
public class Student {
//属性名和列名一样
private Integer id;
private String name;
private String email;
private Integer age;
public Student() {
}
public Student(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", email='" + email + ''' +
", age=" + age +
'}';
}
}
6.3 创建Dao和mapper文件
package com.bjpowernode.dao;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List selectStudents();
}
insert into student values (#{id},#{name},#{email},#{age})
6.4 创建Service及其实现类
package com.bjpowernode.service;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List queryStudents();
}
package com.bjpowernode.service.impl;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
//引用类型
private StudentDao studentDao;
//使用set注入赋值
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public List queryStudents() {
List students = studentDao.selectStudents();
return students;
}
}
6.5 MyBatis配置文件
6.6 Spring配置文件
6.7 前端jsp文件
package com.bjpowernode.dao;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List selectStudents();
}
insert into student values (#{id},#{name},#{email},#{age})
6.4 创建Service及其实现类
package com.bjpowernode.service;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List queryStudents();
}
package com.bjpowernode.service.impl;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
//引用类型
private StudentDao studentDao;
//使用set注入赋值
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public List queryStudents() {
List students = studentDao.selectStudents();
return students;
}
}
6.5 MyBatis配置文件
6.6 Spring配置文件
6.7 前端jsp文件
6.6 Spring配置文件
6.7 前端jsp文件
注册界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
注册学生
注册成功界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
注册成功
6.8 创建Servlet
package com.bjpowernode.controller;
import com.bjpowernode.domain.Student;
import com.bjpowernode.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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strId = request.getParameter("id");
String strName = request.getParameter("name");
String strEmail = request.getParameter("email");
String strAge = request.getParameter("age");
//创建spring的容器对象
// 这么做的话注册一次就会创建一个容器对象
//String config = "applicationContext.xml";
//ApplicationContext ac = new ClassPathXmlApplicationContext(config);
// 这么做的话只有一个容器对象
WebApplicationContext ac = null;
//获取ServletContext中的容器对象,创建好的容器对象,拿来就用
//String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//Object attr = getServletContext().getAttribute(key);
//if(attr != null){
//ac = (WebApplicationContext)attr;
//}
//使用框架中的方法,获取容器对象
ServletContext sc = getServletContext();
ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
System.out.println("容器对象的信息======" + ac);
//获取service
StudentService service = (StudentService) ac.getBean("studentService");
Student student = new Student();
student.setId(Integer.parseInt(strId));
student.setName(strName);
student.setEmail(strEmail);
student.setAge(Integer.valueOf(strAge));
service.addStudent(student);
//返回一个页面
request.getRequestDispatcher("/result.jsp").forward(request,response);
}
}
6.9 web.xml文件
RegisterServlet
com.bjpowernode.controller.RegisterServlet
RegisterServlet
/reg
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
6.10 总结
RegisterServlet com.bjpowernode.controller.RegisterServlet RegisterServlet /reg contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener
6.10 总结
如果用之前做javase项目一样的方式来创建容器对象,那么每一次注册(即进入了servlet)就会新建一个容器对象,而web项目中容器对象应该只需要创建一次,把容器对象给到全局作用域ServletContext中。
怎么实现?
使用监听器,当全局作用域对象被创建时,将容器对象存入ServletContext。
监听器的作用:
①创建容器对象,执行ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
②把容器对象放入到ServletContext。ServletContext.setAttribute(key,ctx);
监听器可以自己创建,也可以是使用框架中提供好的ContextLoaderListener。
ContextLoaderListener监听器的源代码中会创建出一个容器对象:
private WebApplicationContext context;
这个WebApplicationContext类:
public interface WebApplicationContext extends ApplicationContext
ApplicationContext:javase项目中使用的容器对象。
WebApplicationContext:web项目中使用的容器对象。
源代码中还会将这个创建好的对象加入全局作用域对象中:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
key:WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
value:this.context
PS:根据动力节点课程整理,如有侵权,联系删除。



