First, some descriptive narrative of the text:
MVC is no stranger to us, it originated in the 1980s a software design pattern for Smalltalk language has now been widely used. In recent years, with the prevalence of Java, MVC's low coupling, high reusability, maintainability, software project manageability and many other advantages make it very popular in the Java platform, in the meantime, also produced a lot of excellent MVC framework, such as focus on the control layer of STRUTS, Frameworks such as WebWork, Struts2, JSF, and so on, focus on the spring framework for business logic, Hibernate, IBatis, Castor, and jorm that focus on persistent layers. Because of the recent use of an SSI framework, this article is mainly to Strtus2, Spring, ibatis three open-source MVC framework for a summary.
Struts2 mainly comes from the webwork framework, compared with Struts1, in terms of data transmission, STRUTS2 provides a more powerful OGNL tag function, so that it can be directly with the data in the JSP page by defining variables in the action to pass value to each other, The FOrmBean in STRUTS1 is omitted, while in the jump control, Struts2 simplifies the information of the configuration file, makes the exchange between the page and action more concise and intuitive, and facilitates the management of the developer.
Spring functionality is very powerful, such as its control inversion/dependency injection mechanism, eliminating our own writing Factory mode of work, implementation class for we will use the control class, business logic class, data access class, and Jndi or JDBC data source hosting; Spring's support for AOP enables us to chmod. html ' target= ' _blank ' > privilege control, transaction processing, saving a lot of work;
IBatis is a lightweight or Mapping framework that, compared to hibernate, IBatis provides a semi-self-motivated object-relational mapping implementation that developers need to write detailed SQL statement, which provides more free space for system design and facilitates the optimization of SQL statements.
The following diagram is a combination of the three frameworks we use, with a brief introduction.
In the control layer, the STRTUS2 tag function is used to interact directly with The data on the JSP page in the Action. Struts2 provides support for sping When invoking the business Logic Layer application . The developer needs to complete the configuration of the struts.xml and write the Action classes.
In the business logic layer, the dependency injection of the Spring framework is used to host the instance of the business logic class and the DAO class, and in terms of transaction processing, we take advantage of the aspect-oriented transaction function provided by Spring . Make the transaction control of the data out of the data access interface implementation, and in terms of object relational mapping, take advantage of Spring 's hosting of the database connection pool and support for the iBatis framework. The developer needs to complete the configuration of the data source, the configuration of the application*.xml file corresponding to the different modules, and the definition of the business logic interface and the writing of the business logic implementation.
In the persistence layer, using the IBatis provided by the semi-self-active Object Relational mapping implementation, developers need to write detailed SQL statements, for the system design provides a greater freedom of space. In addition, developers need to complete the configuration of Sqlmapconfig.xml and *sqlmap.xml , as well as the definition of DAO interface and the implementation of DAO interface.
In the process of exchanging between the layers, the transmission data is used to transmit and interact the data. The transfer data class corresponds to database table one by one.
The SSI framework can reduce the coupling of our code, enhance the robustness and reusability of the code, and speed up the development, but there are some shortcomings, for example, because the configuration files of three kinds of frameworks are many, but also bring us some inconvenience, especially for smaller applications.
First: The introduction of STRUTS2 spring Ibatis the respective jar packages are not listed here.
Two: Join the configuration file
Let's start with the Web. xml file
Web. XML Loading Process :
1 when you start a Web project, the container (for example: toMCat) reads his configuration file, Web. XML reads two nodes
<listener></listener> and <context-param></context-param>
2 immediately after, the container creates a ServletContext (context) that all parts of the Web project will share this context
3 containers convert <context-param></context-param> to key-value pairs and hand them to ServletContext
4 container creates an instance of the class in <listener></listener>, that is, the listener is created
5 in the listener there will be contextinitialized (Servletcontextevent args) initialization method, obtained in this method:
ServletContext = Servletcontextevent.getservletcontext ();
The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");
Web. XML node Load order
The loading order of the nodes is independent of their order in the Web. xml file. That is, the filter is not written in front of the listener and will be loaded into the filter is finally concluded that: Listener->filter->servlet
At the same time, there is a configuration node: Context-param, which is used to provide ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on are initialized with information about these contexts, so should the Context-param configuration section be written before the Listener configuration section? In fact, the Context-param configuration section can be written in any location, so the real load order is:
Context-param, Listener, filter-a servlet
Loading Spring
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
finally concluded:
The load order of Web. XML is: [context-param, Listener, filter-----spring], The Order of actual program calls between nodes of the same type is called according to the Order of the corresponding mapping.
Open the Web. xml file, depending on the actual need to add for example:
<!--context parameters for log4j and for use with--><context-param> in spring <param-name>webapprootkey</param-name > <param-value>/WEB-INF/log4j.properties</param-value></context-param><!-- Application context parameters, specifying the spring configuration file location--><context-param> <param-name>contextconfiglocation</param-name > <param-value>/WEB-INF/beans.xml</param-value></context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener> <!--listener for initializing spring frame--><listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener>
Here are some of the configuration files that SSI integrates:
1,contextconfiglocation: Spring container needs to be loaded into spring's configuration file when it starts. The default is the Applicationcontext.xml file under the/web-inf folder
Of course it can be placed under classpath and can contain multiple spring profiles, which relies on contextconfiglocation
<!--load a spring configuration file assuming that the file name is Applicationcontext.xml and is in the Web-inf folder without this configuration--
Assume that the configuration of the context-param,spring is not configured in Web. Xml as shown in the example above, and take the initiative to find Applicationcontext.xml under the Web-inf folder. At this point, if you change the name of the Applicationcontext.xml, or remove it, and then start the server, you will get the exception information such as the following:
1.nested exception is java.io.FileNotFoundException:Could not open ServletContext resource [/web-inf/ Applicationcontext.xml]
This confirms its default configuration. In the default configuration, spring will simply go to the Web-inf folder to find the configuration file, instead of looking for classpath.
Suppose we don't want to put the configuration file under the Web-inf folder? In development, a config folder is often created below SRC to hold the configuration file. At this point, the corresponding Param-value is changed to: Classpath:config/applicationcontext.xml.
Be sure to add classpath, which tells spring to go to the Config folder under the Classes folder to find the configuration file.
2. How to start the spring container
Two methods, one with listener to start a load-on-startup Servlet.
<!--configuring Spring listener--><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener>
Another kind
<servlet> <servlet-name>context</servlet-name> <servlet-class> Org.springframework.web.context.contextloaderservlet</servlet-class> <load-on-startup>1</ Load-on-startup></servlet>
3, integrated STRUTS2
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dis Patcher.ng.filter.StrutsPrepareAndExecutefilter</filter-class></filter>< filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></ Filter-mapping>
4,spring Consolidating Ibatis Profiles
id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" ><property name= " Configlocation "> <value>classpath:SqlMapConfig.xml</value></property></bean>
5,struts.xml
<constant name= "struts.objectfactory" value= "Spring"/>
Constant configuration of Struts constants (also available in the struts.properties) file, the Struts object factory is managed by spring.
SSI Framework Summary