在web项目中使用spring,完成学生注册功能
实现步骤:
1.创建maven,web项目
2.加入依赖
拷贝整合mybatis中的依赖,新增jsp,servlet依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wxx</groupId>
<artifactId>ch09-spring-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- 做spring事务用到的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis和spring集成的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- 阿里公司的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!-- 目的是把src/main/java目录中的xml文件包含到输出结果中。输出到classes目录好-->
<resources>
<!--将resources目录下的配置文件编译进classes文件 -->
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 指定jdk的版本-->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.拷贝整合mybatis的代码和配置文件
4.创建一个jsp发起请求,有参数id,name,email,age
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>注册学生</p>
<form action="reg" method="post">
<table>
<tr>
<td>id</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="提交"></td>
</tr>
</table>
</form>
</body>
</html>
5.创建servlet,接收请求参数,调用service,调用dao完成注册
package com.wxx.controller;
import com.service.StudentService;
import com.wxx.domain.Student;
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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
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 ctx=new ClassPathXmlApplicationContext(config);
System.out.println("容器对象信息======"+ctx);
//获取service
StudentService service= (StudentService)ctx.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);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
6.创建一个jsp作为显示结果页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
result.jsp注册成功
</body>
</html>
添加tomcat运行:
提交表单:
跳转:
数据库:
插入成功
每一次请求都会创建一次容器对象,容器对象又会将文件中所有的对象创建一遍,如果文件中对象过多,或者频率过高,会导致内存过载,实际上我们只需要创建一次容器对象。
加入依赖:
<!-- 为了使用监听器对象,加入依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
使用监听器:
1)创建容器对象,执行ApplicationContext ctx=new ClassPathXMLApplicationContext(“applicationContext.xml”);
2)把容器对象放入到ServletContext,ServletContext.setAttribute(key,ctx)
<!-- 注册监听器ContextLoaderListener
被创建对象后,会读取/WEB-INF/spring.xml
为什么要读取文件:因为在监听器中要创建ApplicationContext对象,需要加载配置文件
/WEB-INF/applicationContext.xml就是监听器默认读取的spring配置文件路径
可以修改默认的文件位置,使用context-param重新制定文件的位置
配置监听器:目的是创建容器对象,创建了容器对象,就能把spring.xml配置文件中所有的对象创建好。用户发起请求就可以直接使用对象了。
-->
<context-param>
<!-- contextConfigLocation:表示配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<!-- 自定义配置文件的路径-->
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
修改Servlet中获取容器对象的代码片段:
// 创建spring的容器对象
// String config= "spring.xml";
// ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
WebApplicationContext ctx=null;
//获取ServletContext中的容器对象,创建好的容器对象,拿来就用
String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
Object attr=getServletContext().getAttribute(key);
if (attr!=null){
ctx=(WebApplicationContext)attr;
}
System.out.println("容器对象信息======"+ctx);
或者使用框架中的方法,获取容器对象:
ServletContext sc=getServletContext();
ctx= WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
运行结果与上面相同,但是效率大大提升。