案例一:設計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());
}
}