Original link:
http://www.cnblogs.com/c0liu/p/7469203.html
spring-6 1. Xml <!-- \build\classes\applicationContext.xml --> <?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.atguigu.spring.struts2.beans.Person"> <property name="username" value="atguigu"></property> </bean> </beans> <!-- \src\applicationContext.xml --> <?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.atguigu.spring.struts2.beans.Person"> <property name="username" value="atguigu"></property> </bean> </beans> <!-- \WebContent\WEB-INF\web.xml --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>configLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <listener> <listener-class>com.atguigu.spring.struts2.listeners.SpringServletContextListener</listener-class> </listener> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>com.atguigu.spring.struts2.servlets.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>
2. Java // \src\com\atguigu\spring\struts2\beans\Person.java package com.atguigu.spring.struts2.beans; public class Person { private String username; public void setUsername(String username) { this.username = username; } public void hello(){ System.out.println("My name is " + username); } } // \src\com\atguigu\spring\struts2\listeners\SpringServletContextListener.java package com.atguigu.spring.struts2.listeners; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Lifecycle Listener implementation class SpringServletContextListener * */ public class SpringServletContextListener implements ServletContextListener { /** * Default constructor. */ public SpringServletContextListener() { // TODO Auto-generated constructor stub } / ** * @see of ServletContextListener the contextInitialized # (ServletContextEvent) * / public void the contextInitialized (ServletContextEvent the arg0) { // 1. Get the name of the configuration file Spring. The ServletContext where servletContext = arg0.getServletContext (); String config = servletContext.getInitParameter ( "the configLocation" ); // 1. Create IOC container the ApplicationContext CTX = new new the ClassPathXmlApplicationContext (config); // 2. the IOC container on an attribute of ServletContext. servletContext.setAttribute ( "the ApplicationContext" , CTX); } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } } // \src\com\atguigu\spring\struts2\servlets\TestServlet.java package com.atguigu.spring.struts2.servlets; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import com.atguigu.spring.struts2.beans.Person; /** * Servlet implementation class TestServlet */ public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { // 1. IOC containers obtained from the application domain objects referenced in the ServletContext where servletContext = GetServletContext (); the ApplicationContext CTX = (the ApplicationContext) servletContext.getAttribute ( "the ApplicationContext" ); // 2. give the desired container from the IOC of the bean the Person Person = ctx.getBean (the Person. class ); person.hello (); } }
3. Jsp <!-- \WebContent\index.jsp --> <%@ 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>Insert title here</title> </head> <body> <a href="TestServlet">TestServlet</a> </body> </html>
Reproduced in: https: //www.cnblogs.com/c0liu/p/7469203.html