Spring(六) spring的web项目
文章目录
一、Web 项目使用 Spring 的问题
在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring 容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象。
二、步骤
1.新建一个 Maven Project,web项目
2.pom文件中加入依赖
<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依赖 ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--是做spring事务用到的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--mybatis与spring集成-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--阿里公司的druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖 -->
<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-web</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
</dependencies>
<build>
<!--目的是吧src/main/java目录中的xml文件包含到输出结果中。-->
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</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.复制代码,配置文件
将 spring-mybatis 项目中以下内容复制到当前项目中:
(1)Service 层、Dao 层全部代码
(2)配置文件 applicationContext.xml 及 jdbc.properties,mybatis.xml
(3)pom.xml
(4)加入 servlet ,jsp 依赖
4.建立index页面 与 result页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生注册</title>
</head>
<body>
<center>
<p>学生注册</p>
<form action="reg" method="post">
<table border="2">
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
男<input type="radio" name="sex" value="男">
女<input type="radio" name="sex" value="女">
</td>
</tr>
<tr>
<td>
邮箱:
</td>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
<td>
<input type="reset" value="重置">
</td>
<td>
<input type="submit" value="注册">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<p style="color: red; font-size: 40px">注册成功</p>
</body>
</html>
5.定义RegisterServlet
package com.controller;
import com.entity.Users;
import com.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: LvHaoIT (asus)
* Date: 2021/6/5
* Time: 21:55
*/
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");//获取字符集解码
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
//创建spring容器
// String config = "applicationContext.xml";
// ApplicationContext ac = new ClassPathXmlApplicationContext(config);
WebApplicationContext ac = null;
//获取ServletContext中的容器对象,创建好的容器对象
// ServletContext sc = getServletContext();
ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
UserService userService = (UserService) ac.getBean("userService");
int result = userService.addUsers(new Users(userName, password, sex, email));
System.out.println("result=" + result);
if (result == 1) {
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
}
6.web.xml注册Servlet
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
7.配置tomcat运行
三、程序中的不足
当需要使用spring容器时,每次使用都需要重新运行一遍applicationContext中的所有内容,耗时,并且如果次数多了,jvm垃圾回收机制没有及时销毁,会将内存耗尽
问题代码:
//创建spring容器
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
如何解决问题
对于 Web 应用来说,ServletContext 对象是唯一的,一个 Web 应用,只有一个 ServletContext 对象,该对象是在 Web 应用装载时初始化的。若将 Spring 容器的创建时机, 放在 ServletContext 初始化时,就可以保证 Spring 容器的创建只会执行一次,也就保证了 Spring 容器在整个应用中的唯一性。
当 Spring 容器创建好后,在整个应用的生命周期过程中,Spring 容器应该是随时可以被访问的。即,Spring 容器应具有全局性。而放入 ServletContext 对象的属性,就具有应用的 全局性。所以,将创建好的 Spring 容器,以属性的形式放入到 ServletContext 的空间中,就 保证了 Spring 容器的全局性。
上述的这些工作,已经被封装在了如下的 Spring 的 Jar 包的相关 API 中: spring-web-5.2.5.RELEASE
解决步骤
1.添加maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
2.注册监听器ContextLoaderListener(在web.xml中)
<listener>
<listenerclass>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3.指定spring配置文件位置< context-param>
<!--注册监听器
监听器被创建对象后,会读取/WEB-inf/applicationContext.xml
默认的,必须要在这个文件目录下
但是可以修改 ,使用context-param重新指定路径
-->
<context-param>
<!--contextConfigLocation:表示配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<!--自定义路径-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
4.获取spring容器对象
工具类 WebApplicationContextUtils 有一个方法专门用于从 ServletContext 中获取 Spring 容器对象:getRequiredWebApplicationContext(ServletContext sc)
ServletContext sc = getServletContext();
ac = WebApplicationContextUtils.
getRequiredWebApplicationContext(sc);
(spring完)
2021.6.5 23:32