案例一:设计Web程序,在jsp页面获取SpringIoC,并显示有关信息。
分析:首先在web.xml中配置信息,使之自动创建并装载SpringIoC容器,然后建立一个JavaBean,并包含获取信息的方法,最后设计jsp页面,获取信息并显示。
代码:
(1)创建web工程,并在web.xml中配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<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)设计JavaBean
package com.edu.spring.web.beans;
public class Person {
private String username;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public String geyHello(){
return "My name is " + username;
}
}
(3)创建Spring的配置文件(applicationContext.xml),并添加配置Person的Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person"
class="com.edu.spring.web.beans.Person">
<property name="username" value="张三"></property>
</bean>
</beans>
(4)设计jsp页面(index.jsp)
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="com.edu.spring.web.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Web 第一个案例</title>
</head>
<body>
<%
//1. 从 appication 域对象中得到 IOC 容器的实例
ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(application);
//2. 从 IOC 容器中得到 bean
Person person = (Person) ctx.getBean(Person.class);
//3. 使用 bean
%>
从Bean中获取的信息:<%=person.geyHello()%>
</body>
</html>
案例二:通过servlet获取SpringIoC,并显示有关信息
分析:实现过程与案例一相同,只是将jsp页面替换为servlet即可。即设计servlet,并采用注释配置方式配置servlet
package com.edu.spring.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.edu.spring.web.beans.Person;
@WebServlet("/SpringIoC")
public class SpringIoC extends HttpServlet {
private static final long serialVersionUID = 1L;
public SpringIoC() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost( request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// (1)首先通过request对象,获取web服务器容器
ServletContext sc = request.getServletContext();
//(2)利用Spring框架提供的静态方法,从Web服务器中获取 Spring容器
WebApplicationContext wact = WebApplicationContextUtils
.getWebApplicationContext(sc);
// (3) 从 IOC 容器中得到 bean
Person person = (Person) wact.getBean(Person.class);
// (4) 使用 bean
response.setCharacterEncoding("GBK");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println(person.geyHello());
}
}