Spring Integrated STRUS2 Simple Application Summary

Source: Internet
Author: User

itself Strus2 have not touched, so this piece of learning smattering, normal not integrated still not learn (continue to learn)

Step

1. Create a Web project

2. Introduction of JAR package in/web-inf/lib

Asm-3.3.jar
Asm-commons-3.3.jar
Asm-tree-3.3.jar
Com.springsource.net.sf.cglib-2.2.0.jar
Com.springsource.org.aopalliance-1.0.0.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Commons-fileupload-1.3.jar
Commons-io-2.0.1.jar
Commons-lang3-3.1.jar
Commons-logging-1.1.3.jar
Freemarker-2.3.19.jar
Javassist-3.11.0.ga.jar
Log4j-1.2.17.jar
Ognl-3.0.6.jar
Spring-aop-4.0.0.release.jar
Spring-aspects-4.0.0.release.jar
Spring-beans-4.0.0.release.jar
Spring-context-4.0.0.release.jar
Spring-core-4.0.0.release.jar
Spring-expression-4.0.0.release.jar
Spring-jdbc-4.0.0.release.jar
Spring-orm-4.0.0.release.jar
Spring-tx-4.0.0.release.jar
Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar
Struts2-core-2.3.15.3.jar
Struts2-spring-plugin-2.3.15.3.jar Red shading must be added to the jar so that it can be integrated
Xwork-core-2.3.15.3.jar

3. Write test code

Person entity class

 Package com.strus.entity;  Public class Person {    private  String username;      Public void Setusername (String username) {        this. Username = username;    }           Public void say () {        System.out.println ("hellow," +username);}    }

Dao

 Package Com.strus.dao;  Public class Persondao {    public  String Save () {        System.out.println ("Persondao save ...") );         return "Success!" ;    }}

Action

 package   com.strus.action;  import   Com.strus.dao.PersonDao;  public  class   Personserver { private   Persondao Persondao;  public  void   Setpersondao (Persondao Persondao) { this .    Persondao = Persondao;  public  void   execute () {System.out.println (" personserver execute ...).        ");    Persondao.save (); }}

4. Create a spring Bean.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 ">    <BeanID= "Person"class= "Com.strus.entity.Person">        < Propertyname= "username"value= "Xiaoqiang"></ Property>    </Bean>    <BeanID= "Persondao"class= "Com.strus.dao.PersonDao" scope= "Prototype" ></Bean>    <!--Note: When configuring the Struts2 Action in an IOC container, the scope property needs to be configured with a value of prototype -    <BeanID= "Personserver"class= "Com.strus.action.PersonServer">        < Propertyname= "Persondao"ref= "Persondao"></ Property>    </Bean></Beans>

5, copy Strus.xml to SRC, and bean.xml peer file

I don't know why I have to copy this file, and when I learn strus2, I guess I know what happened.

Just learn not to remember Haha, first said to modify the location, personally think is the action tag is very important

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>    <constantname= "Struts.enable.DynamicMethodInvocation"value= "false" />    <constantname= "Struts.devmode"value= "true" />    < Packagename= "Default"namespace="/"extends= "Struts-default">                <!--when spring consolidates Struts2, the class of the spring Action configured in Struts2 needs to point to the ID of the bean in the IOC container, -    <action  name= "Person-save"  class= "Personserver " ><result>/success.jsp</result>  </action>                                      </ Package></Struts>

6. Web. XML creation

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns: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_3_0.xsd"ID= "webapp_id"version= "3.0">        <!--Configure the name and location of the Spring configuration file -    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:bean.xml</Param-value>    </Context-param>        <!--start the Servletcontextlistener of the IOC container -    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>                <!--Configuring the Filter for the Struts2 -    <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>

7. Create tests, index.jsp and success.jsp

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">"Content-type"Content="text/html; Charset=utf-8"><title>insert title here</title>"Person-save">person save</a> </body>

success.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">"Content-type"Content="text/html; Charset=utf-8"><title>insert title here</title>

The compilation runs as follows:

Click: Person Save

Summary of Ideas:

1. How does Spring work in WEB apps?

1). Additional jar packages are required:

Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar

2). Spring configuration file, no difference

3). How do I create an IOC container?

①. Non-WEB apps are created directly in the Main method
②. The IOC container should be created when the WEB application is loaded by the server:

Create an IOC container in the servletcontextlistener#contextinitialized (Servletcontextevent sce) method.

③. How do I access the IOC container in other components of the WEB app?

After you create an IOC container in the servletcontextlistener#contextinitialized (Servletcontextevent sce) method, you can place it in the
A property of the ServletContext (that is, the application domain).

④. In fact, the name and location of the Spring configuration file should also be configurable! It is more appropriate to configure it to the initialization parameters of the current WEB application.

4). Use Spring in a WEB environment

①. jar packages that require additional addition:

Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar

②. Spring configuration file, not unlike a non-WEB environment

③. You need to include the following configuration in the Web. xml file:

<!--Configure the name and location of the Spring configuration file--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!--launch the IOC container Servletcontextlistener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2. How does Spring integrate Struts2?

1). Integration Goals? Enable IOC containers to manage Struts2 's action!

2). How do I integrate?

①. Normal Join STRUTS2

②. Configuring the Struts2 Action in the Spring IOC container
Note: When configuring the Struts2 Action in an IOC container, the scope property needs to be configured with a value of prototype

<bean id= "Personaction"
Class= "Com.atguigu.spring.struts2.actions.PersonAction"
Scope= "Prototype" >
<property name= "Personservice" ref= "Personservice" ></property>
</bean>

③. Configuring the Struts2 configuration file: The class attribute of the action node needs to point to the ID of the bean in the IOC container

<action name= "Person-save" class= "Personaction" >
<result>/success.jsp</result>
</action>

④. Join Struts2-spring-plugin-2.3.15.3.jar

3). Integration principle: By adding Struts2-spring-plugin-2.3.15.3.jar, the Struts2 will be first removed from the IOC container
Gets an instance of the Action.

if (Appcontext.containsbean (Beanname)) {
o = Appcontext.getbean (beanname);
} else {
Class Beanclazz = getclassinstance (beanname);
o = Buildbean (Beanclazz, Extracontext);
}

Spring Integrated STRUS2 Simple Application Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.