• Spring 教程07


    spring-7
    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="spring"></property>    
        </bean>
        
        <bean id="personService"
            class="com.atguigu.spring.struts2.services.PersonService"></bean>
        
        <!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype -->
        <bean id="personAction" 
            class="com.atguigu.spring.struts2.actions.PersonAction"
            scope="prototype">
            <property name="personService" ref="personService"></property>  
        </bean>
        
    </beans>
    
    <!-- uildclassesstruts.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
            
            <!--  
                Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id
            -->
            <action name="person-save" class="personAction">
                <result>/success.jsp</result>
            </action>
            
        </package>
    
    </struts>
    
    <!-- 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="spring"></property>    
        </bean>
        
        <bean id="personService"
            class="com.atguigu.spring.struts2.services.PersonService"></bean>
        
        <!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype -->
        <bean id="personAction" 
            class="com.atguigu.spring.struts2.actions.PersonAction"
            scope="prototype">
            <property name="personService" ref="personService"></property>  
        </bean>
        
    </beans>
    
    <!-- srcstruts.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
            
            <!--  
                Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id
            -->
            <action name="person-save" class="personAction">
                <result>/success.jsp</result>
            </action>
            
        </package>
    
    </struts>
    
    <!-- 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">
    
        <!-- 配置 Spring 配置文件的名称和位置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        
        <!-- 启动 IOC 容器的 ServletContextListener -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <!-- 配置 Struts2 的 Filter -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    </web-app>
    2.    Java
    
    
    // srccomatguiguspringstruts2actionsPersonAction.java
    package com.atguigu.spring.struts2.actions;
    
    import com.atguigu.spring.struts2.services.PersonService;
    
    public class PersonAction {
        
        private PersonService personService;
        
        public void setPersonService(PersonService personService) {
            this.personService = personService;
        }
        
        public String execute(){
            System.out.println("execute....");
            personService.save();
            return "success";
        }
        
    }
    
    // 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);
        }
        
    }
    
    // srccomatguiguspringstruts2servicesPersonService.java
    package com.atguigu.spring.struts2.services;
    
    public class PersonService {
        
        public void save(){
            System.out.println("PersonService's save....");
        }
        
    }
    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="person-save">Person Save</a>
        
    </body>
    </html>
    
    <!-- WebContentsuccess.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>
        
        <h4>Success Page</h4>
        
    </body>
    </html>
    
    <!-- WebContent	est.jsp -->
    <%@page import="com.atguigu.spring.struts2.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>Insert title here</title>
    </head>
    <body>
        
        <% 
            //1. 从 appication 域对象中得到 IOC 容器的实例
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
            
            //2. 从 IOC 容器中得到 bean
            Person person = ctx.getBean(Person.class);
            
            //3. 使用 bean
            person.hello();
        %>
        
    </body>
    </html>
  • 相关阅读:
    [CSP-S模拟测试]:party?(霍尔定理+最小割+树链剖分)
    [CSP-S模拟测试]:marshland(最大费用可行流)
    [CSP-S模拟测试]:Revive(点分治)
    [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)
    [CSP-S模拟测试]:Lost My Music(凸包)
    [CSP-S模拟测试]:God Knows(线段树维护单调栈)
    [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)
    [CSP-S模拟测试]:gcd(莫比乌斯反演)
    [CSP-S模拟测试]:water(BFS)
    BZOJ3462 DZY Loves Math II 【多重背包 + 组合数】
  • 原文地址:https://www.cnblogs.com/c0liu/p/7469205.html
Copyright © 2020-2023  润新知