spring–创建web项目
web项目不同于javase项目,web项目没有main方法,而javase项目都有一个main方法,所以它们创建spring容器的方式都有所不同。
示例代码:
这次的示例代码,我们使用spring整合mybatis的项目数据库表的设计,完成一个注册学生信息的功能,注册成功页面不放出来了。项目里面mybatis主配置文件,数据库配置文件和applicationContext.xml文件和整合mybatis的项目是一样的。
需要添加的依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<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>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
项目文件结构:
注册页面(index.jsp):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>注册学生</p>
<form action="registerServlet" 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" value="注册学生"></td>
</tr>
</table>
</form>
</body>
</html>
1.web.xml配置文件,这个文件里面配置监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>registerServlet</servlet-name>
<servlet-class>com.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerServlet</servlet-name>
<url-pattern>/registerServlet</url-pattern>
</servlet-mapping>
<!--注册监听器ContexLoaderListener
监听器被创建后,会读取/WEB-INF/applicationContext.xml
为什么要读取这个文件:因为监听器需要创建ApplicationContext对象,需要加载配置文件
/WEB-INF/applicationContext.xml是监听器默认读取的spring配置文件路径
可以修改默认的文件位置,使用context-param重新指定文件的位置
监听器会创建web容器对象WebApplicationContext,容器对象创建好了,配置文件里面的所有对象都会创建好,
然后将WebApplicationContext对象放入到全局作用域ServletContext中,
key是 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
-->
<context-param>
<!--表示配置文件的位置的-->
<param-name>contextConfigLocation</param-name>
<!--自定义配置文件的路径-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--创建监听器的代码-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.写一个servlet程序,这个程序里面接收前端的消息,从servletContext全局作用域中获取容器对象,再从容器对象中获取service对象,调用service对象的方法完成学生的注册:
package com.controller;
import com.domain.Student;
import com.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 {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收前端页面发来的消息
String id = req.getParameter("id");
String name = req.getParameter("name");
String email = req.getParameter("email");
String age = req.getParameter("age");
//获取ServletContext中的容器对象,这是监听器创建好的,放入到全局作用域中的,直接取用即可
//使用框架里面的方法,获取容器对象
ServletContext servletContext = getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
System.out.println("容器对象的信息:" + webApplicationContext);
//从容器中获取service对象
StudentService service = webApplicationContext.getBean("studentService", StudentService.class);
Student student = new Student();
student.setId(Integer.parseInt(id));
student.setName(name);
student.setAge(Integer.parseInt(age));
student.setEmail(email);
service.addStudent(student);
//给一个页面
req.getRequestDispatcher("/result.jsp").forward(req, resp);
}
}