zoukankan      html  css  js  c++  java
  • Spring 教程06

    spring-6
    1.    Xml
    
    
    <!-- uildclassesapplicationContext.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>
    
    <!-- srcapplicationContext.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>
    
    <!-- WebContentWEB-INFweb.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
    
    
    // srccomatguiguspringstruts2eansPerson.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);
        }
        
    }
    
    // srccomatguiguspringstruts2listenersSpringServletContextListener.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 ServletContextListener#contextInitialized(ServletContextEvent)
         */
        public void contextInitialized(ServletContextEvent arg0) {
            //1. 获取 Spring 配置文件的名称. 
            ServletContext servletContext = arg0.getServletContext();
            String config = servletContext.getInitParameter("configLocation");
            
            //1. 创建 IOC 容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
            
            //2. 把 IOC 容器放在 ServletContext 的一个属性中. 
            servletContext.setAttribute("ApplicationContext", ctx);
        }
    
        /**
         * @see ServletContextListener#contextDestroyed(ServletContextEvent)
         */
        public void contextDestroyed(ServletContextEvent arg0) {
            // TODO Auto-generated method stub
        }
        
    }
    
    // srccomatguiguspringstruts2servletsTestServlet.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) throws ServletException, IOException {
            //1. 从 application 域对象中得到 IOC 容器的引用
            ServletContext servletContext = getServletContext();
            ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
            
            //2. 从 IOC 容器中得到需要的 bean
            Person person = ctx.getBean(Person.class);
            person.hello();
        }
    
    }
    3.    Jsp
    
    
    <!-- WebContentindex.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>
  • 相关阅读:
    Atitit 经济学常见的流派 古典主义与凯恩斯主义
    Atitit 学习方法 体系化学习方法 Excel 科目,分类,专业 三级分类。。 知识点。。 课程就是每一个知识点的详细化。。 比如经济学 类别 专业 xx概论知识点 3、金
    atiitt it学科体系化 体系树与知识点概念大总结.xlsx
    Atitit 减少财政支出普通人如何蹭政府补贴措施 attilax大总结.docx
    Atitit 信用管理概论 attilax学习心得
    Atitit.月度计划日程表 每月流程表v5
    Atitit 企业6大职能 attilax总结
    Atitit 常见每日流程日程日常工作.docx v8 ver ampm imp 签到 am y 天气情况检查 am y 晨会,每天或者隔天 am 每日计划(项目计划,日计划等。 am
    Atitit 财政赤字解决方案
    Atitit 建设自己的财政体系 attilax总结 1.1. 收入理论 2 1.2. 收入分类 2 1.3. 2 1.4. 非货币收入 2 1.5. 2 1.6. 降低期望 2 1.7.
  • 原文地址:https://www.cnblogs.com/c0liu/p/7469203.html
Copyright © 2011-2022 走看看