Spring Web 项目 本身依托于 Web容器 此处以Tomcat为例
本身项目就归属于Tomcat来管理 此时 在此容器的基础上去融合了 Spring Context
相当于要在Tomcat启动后 加载项目时 将Spring容器 嵌入到当前Tomcat容器中
后期是Tomcat容器来托管 Spring容器 之后Spring容器来管理项目中的相关内容
SpringWeb提供了两种方式来让Web容器管理Spring容器 :
ContextLoaderListener 或者 ContextLoaderServlet
建议使用ContextLoaderListener 来加载Spring容器
application.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.liqk"/>
</beans>
web.xml
<!--设置初始化参数 在Tomcat启动时 能够创建该参数 供后续ServletContext使用 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
议使用ContextLoaderListener 来加载Spring容器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<?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">
<!--设置初始化参数 在Tomcat启动时 能够创建该参数 供后续ServletContext使用 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>com.wangbx.servlet.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/stu/info</url-pattern>
</servlet-mapping>
<!--
使用 Springweb中提供的过滤器来做请求字符集过滤操作 无需手动开发相关过滤器
-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
Spring Web 项目 本身依托于 Web容器 此处以Tomcat为例
本身项目就归属于Tomcat来管理 此时 在此容器的基础上去融合了 Spring Context
相当于要在Tomcat启动后 加载项目时 将Spring容器 嵌入到当前Tomcat容器中
后期是Tomcat容器来托管 Spring容器 之后Spring容器来管理项目中的相关内容
SpringWeb提供了两种方式来让Web容器管理Spring容器 :
ContextLoaderListener 或者 ContextLoaderServlet
建议使用ContextLoaderListener 来加载Spring容器
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Spring Web 提供了一个能够在一定程度上优化项目的监听器 该监听器的目的是尽量防止内存泄漏-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>
在非Web项目中 根据具体的配置方式 通过 ClassPathHXMLApplicationContext 或者 AnnotationApplicationContext来获取容器 此时在Web项目中如果要获取Spring容器 需要从Web容器中获取相关Spring容器对象
//@WebServlet("/sss")
public class StudentServlet extends HttpServlet {
/*
* 在非Web项目中 根据具体的配置方式 通过 ClassPathHXMLApplicationContext 或者 AnnotationApplicationContext来获取容器
* 此时在Web项目中如果要获取Spring容器 需要从Web容器中获取相关Spring容器对象
* */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* Springweb 提供 String处理工具 StringUtils
* */
String filename = "abc_01.demo.img";
String fn = StringUtils.getFilename(filename);
String en = StringUtils.getFilenameExtension(filename);
System.out.println("文件名称:"+fn+",后缀名:"+en);
//能够验证不为null 且 不为空字符 "" 不能判定 " "
System.out.println(StringUtils.hasLength(filename));
//在hasLength的基础上 判定字符串中的已存在内容 不能为空白
System.out.println(StringUtils.hasText(filename));
/*
* 当前请求抵达Servlet的doGet方法时 通过该方法内部获取Spring的容器
* 并通过容器获取当前容器内管理的Bean对象 Student
* */
ServletContext sc = req.getServletContext();
/*Spring Web 中提供了一个方便从Tomcat容器中获取Spring容器的工具类
* WebApplicationContextUtils
* */
WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(sc);
Student stu = context.getBean("student", Student.class);
System.out.println(stu);
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
if(null == name){
System.out.println("未获取用户名");
}else {
if ("admin".equals(name)) {
System.out.println("欢迎管理员登录");
} else {
System.out.println("欢迎" + name + "登录");
}
}
}
}
entity
@Component
public class Student {
@Value("李四")
private String name;
@Value("18")
private int age;
@Value("true")
private boolean gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}