Spring2.5, STRUTS2, Ibatis Development Framework (SSI) __ Frame

Source: Internet
Author: User
Tags i18n log4j

First, Frame download

1.1 STRUTS2 Framework

Struts2 framework developed in WebWork, now donated to the Apache Open source organization, the latest version of the STRUTS2 framework can be located in the official Apache website STRUTS2 Project, STRUTS2 framework of the project home address is: http:// struts.apache.org/, download page address: http://struts.apache.org/download.cgi

, open the download address after you choose the latest release of the version can be a novice to download the full distribution version, such as http://struts.apache.org/download.cgi# The Struts-2.0.14-full.zip in the struts2014.

To enable STRUTS2 to support JSON-formatted return data, you also need to download the JSON plugin and download the address

http://code.google.com/p/jsonplugin/, download file as Jsonplugin-0.31.jar file.

1.2 Spring2.5 Framework

The official website of the Spring framework: http://www.springframework.org/, download page: http://www.springsource.org/download, open the download page and choose Get the Latest Spring releases the release package in here to download and note that there are many frameworks included in the Spring official web site, including process processing, security, Web services, and many other frameworks, the framework we need to download is a version of the spring framework, Not the other frames.

1.3 Ibatis Framework

The homepage of the Ibatis framework is http://ibatis.apache.org/, select the For Java connection in the left software in the open Web interface, and select the download connection in the Open interface.

1.4 Other (database buffer pool framework, etc.)

If you need to use the MS SQL Server database, you will also need to add the Jtds JDBC driver to your project, download the address http://jtds.sourceforge.net/, locate the download connection, and download the message as prompted. Download the DBCP database buffer pool Framework, download the address for http://commons.apache.org/dbcp/, download the latest version, because the DBCP framework is part of the many frameworks of the Apache open source software organization, to make DBCP work, Also need to Common-pool frame, to the Apache official website to download.

Add the Spring Struts Ibatis framework to the project

2.1 Preparation work

Unzip the downloaded three frames separately.

Create a new Web Project.

2.2 Adding the Spring framework to your project

Copy Spring.jar from the Spring-framework-2.5.6-with-dependencies\spring-framework-2.5.6\dist directory after spring decompression to the web-inf of the Web project The/lib directory.

Copy the log4j-related jar files in the spring-framework-2.5.6-with-dependencies\spring-framework-2.5.6\lib\log4j to the Web-inf/lib directory in the project.

Open the Web.xml file in the Web-inf directory in the Web project, add and combine the actual project to configure the following

<!--thought it was the application name-->

<display-name>JI</display-name>

<!--The following is the application description descriptive text-->

<description>job integration</description>


<!--context parameters for use in log4j and in spring-->

< context-param>

	<param-name>webAppRootKey</param-name>

	<param-value>ji.root</ param-value>

</context-param>


<!--application context parameters, specifying the configuration parameter file location used by the LOG4J log framework-->

< context-param>

	<param-name>log4jConfigLocation</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>

such as/web-inf/spring/applicationcontext.xml

</param-value>
</context-param>

<!--listener,-->

<listener>

for initializing log4j log framework <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</ Listener>

<!--listener, for initializing Spring framework-->

<listener>

<listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>

</listener>


2.3 Adding a STRUTS2 framework to your project

Open the STRUTS2 Framework directory after decompression, locate the \struts-version-all\struts-version \lib directory, and copy the *.jar file to the Web-inf/lib directory of the Web project.

Copy Jsonplugin-0.31.jar to the Web-inf/lib directory of the project.

Open the Web-inf/web.xml file in your Web project and add the following:

<!--filter, Struts2 used, for destroying certain objects that are no longer in use--> <filter> <filter-name>struts-clean Up</filter-name> <filter-class>org.apache.struts2.dispatcher.actioncontextcleanup</filter-class&

    Gt

        </filter> <!--filter, Struts2 used--> <filter> <filter-name>struts</filter-name>

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!--The following is a filter used by STRUTS2 to purge or destroy certain objects, to ensure that this item is configured before the Struts filter mapping--> <filter-mapping> <filter-name>

    

;struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

        <!--struts2 for filter mapping--> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 


Locate the source directory of the project, typically in the Eclipse development tool is the SRC directory, add the following several files (note case), struts.properties,struts.xml,globalmessages.properties, which

The contents of the Struts.properties file are:

Struts.custom.i18n.resources=globalmessages

struts.objectfactory=spring

Struts.objectfactory.spring.autowire=type

struts.ui.theme=simple

struts.locale =zh_CN

Struts.i18n.encoding=utf-8

struts.configuration.xml.reload=true

struts.multipart.parser=jakarta 

struts.multipart.maxsize=10000000

Struts.devmode=false


The contents of the Globalmessages.properties file are:

Smspagetitle=\u5b89\u5fbd\u7701\u8840\u6db2\u4fe1\u606f\u77ed\u4fe1\u5e73\u53f0 login.error=\u60a8\u8f93\u5165\ U7684\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff01 userid.required=\u60a8\u7684\u7528\u6237\u540d\ U4e0d\u80fd\u4e3a\u7a7a\uff01\uff01 passward.required=\u60a8\u7684\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01\ Uff01 datebase.error=\u6570\u636e\u5e93\u64cd\u4f5c\u9519\u8bef\uff01\uff01\uff01 validCode_error=\u9a8c\u8bc1\ U7801\u9519\u8bef\uff01\uff01 retistsdk.error=\u6ce8\u518csdk\u5931\u8d25\uff01\u8bf7\u68c0\u67e5\u5e8f\u5217\

U53f7\u548c\u5bc6\u7801\uff01\uff01 retistsdk.success=\u8f6f\u4ef6sdk\u6ce8\u518c\u6210\u529f\uff01\uff01\uff01 Logoutsdk.success=\u60a8\u5df2\u6210\u529f\u6ce8\u9500sdk\uff01\uff01 logoutsdk.error=\u6ce8\u9500sdk\u5931\ U8d25\uff0c\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\uff01\uff01\uff01 chargup.error=\u5145\u503c\u5931\u8d25\uff0c\ U8bf7\u68c0\u67e5\u5361\u53f7\u548c\u5bc6\u7801\uff01\uff01 sdknotreg=\u7cfb\u7edfsdk\u6ca1\u6709\u6ce8\u518c\Uff01\uff01\uff01\uff01 Struts.messages.error.uploading=\u6587\u4ef6\u65e0\u6cd5\u6b63\u5e38\u4e0a\u4f20

struts.messages.error.file.too.large=\u6587\u4ef6\u5927\u5c0f\u8d85\u8fc7\u6700\u5927\u5141\u8bb8\u503c Struts.messages.error.content.type.not.allowed=\u6587\u4ef6\u7c7b\u578b\u4e0d\u5728\u4e0a\u4f20\u6587\u4ef6\
 U5141\u8bb8\u7c7b\u578b\u4e2d

The last file in


is the content in Struts.xml, which is the parameter profile of struts, reference configuration:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.0//en" "Http://struts.a Pache.org/dtds/struts-2.0.dtd "> <struts> <constant name=" struts.i18n.encoding "value=" UTF-8 "></" constant> <package name= "Login" extends= "Struts-default" > <action name= "Login" class= "EDU.XW.JI.ACTION.L"

	Oginaction "method=" Login "> <result name=" Success ">/dev_test/login.jsp</result> </action> </package> <package name= "Ji_common" extends= "Json-default" > <interceptors> <interceptor-st Ack name= "Mydefaultstack" > <interceptor-ref name= "defaultstack"/> </interceptor-stack> </in terceptors> <default-interceptor-ref name= "Mydefaultstack" ></default-interceptor-ref> < global-results> <result name= "error" >/pages/errorMessage.jsp</result> <result name= "warN ">/pages/warnMessage.jsp</result> </global-results> </package> <include file=" edu/xw/ji/a
 Ction/dict/struts2_dict.xml "/> </struts>


Note that there are two example package in the above struts.xml configuration file, the package named login is inherited from the Struts-default package and cannot implement the JSON-formatted action return, followed by the Ji_ The common is inherited from the Json-default package and can be inherited from the Ji_common package later in the action written by all program developers in the project using the return result in JSON format.

2.4 Adding a Ibatis framework to your project

Locate the Ibatis-2.3.4.726\lib directory in the uncompressed Ibatis package and copy the. jar file inside the Web-inf/lib directory in the project.

2.5 Adding the database buffer pool framework

Copy the Commons-dbcp-x.x.x.jar files from the DBCP buffer pool framework to the Web-inf/lib directory in the project, copy Common-pool related jar files that need to be used when working on the DBCP framework to the web-inf/in the project Lib directory, DBCP is configured in the project to use in spring, so the next series of work is related to the configuration of spring.

Iii. Integration of spring2.5,struts2,ibatis,dbcp,log4j

3.1 What ' s the point.

Why "integrate" these frameworks. In the program code by the programmer responsible for handling and control everything is not very good. What is the benefit of having so many frames to work with? The answer is only one, "easy" saves programmers time to develop programs.

The key to consolidating these frameworks is that spring,spring is the glue that all of these frameworks work together, and the focus is on bringing the Web MVC Framework, ORM Framework, database buffer pool framework, log framework, and so on to spring management, These frameworks are used by spring provisioning to work together.

3.2 Spring How to get a Web project loaded.

One of the configuration parameters in section 2.2 is used to enable our Web project to automatically load the spring framework and its parameter configuration files at startup, that is, the following line of configuration parameters. Listener

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</ Listener>


3.3 Spring's parameter configuration file is loaded automatically.

After you configure spring auto load, you should configure the load location of the spring parameter file in the Web-inf/web.xml of the Web project, which is required in section 2.2

<!--application context parameters, specifying the spring configuration file location-->

<context-param>

	<param-name>contextconfiglocation </param-name>

	<param-value>/web-inf/spring/applicationcontext.xml/web-inf/spring/ibatis_ Dao.xml/web-inf/spring/struts_action.xml</param-value>

</context-param>


After <param-value> configure the path to the spring parameter file, when multiple spring parameter profiles exist, each parameter file is separated by a space.

3.4 How to let Spring manage Struts2.

For spring to manage the creation of the Struts2 action Bean, you need to specify in the Struts.properties file in the project's SRC directory

Struts.objectfactory=spring

Struts.objectfactory.spring.autowire=type


This is the two lines contained in the 2.3 subsection configuration parameter.

3.5 How to get spring to manage the database buffer pool.

Database buffer pools such as DBCP or C3P0 can be configured in spring's parameter configuration files, such as configuring the DBCP database buffer pool in a project to specify parameter configurations in the following format:

<bean id= "DataSource"

class= "Org.apache.commons.dbcp.BasicDataSource"

destroy-method= "Close" >

	<property name= "driverclassname"

		value= "${jdbc.driverclassname}"/> <property name=

	"url" Value= "${jdbc.url}"/> <property name= "username" value=

	"${jdbc.username}"/> <property name=

	" Password "value=" ${jdbc.password} "/> <property name=" initialsize "

	value=" 4 "/> <property name="

	Maxactive "value="/> <property name= "Testwhileidle"

	value= "true"/> <property "

	Validationquery "                                                 value=" select count (0) from t_db_history where 1=2 "/> <property name=

	" Testonborrow " Value= "true"/>

</bean>


Some of the above parameter values are configured in the form of the $ symbol plus {}, which requires a bean configured in spring to load the. propertis file, as in the following format:

<bean id= "Propertyconfigurer"

class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">

	<property name=" Locations " >

		<list>

			<value>WEB-INF/jdbc.properties</value>

		</list>

	</ Property>

</bean>


Following the list element in the configuration parameter above, follow the. properties file.

Of course, if you do not want to write parameter files outside of spring, you can also directly connect the database URL, user, password, and so on when configuring DBCP to die in spring.

3.6 If the Ibatis framework is integrated.

Ibatis Framework to work, you need to specify a global ibatis parameter profile, where you need to tell the Ibatis framework in spring, where you can find the parameter files you need to work with, and the spring framework encapsulates common ORM frameworks, and so on. So to configure the Ibatis framework in spring, the class that is actually configured is a class in the spring framework, as follows:

<bean id= "sqlmapclient"

class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >

	< Property Name= "Configlocations" value= "Web-inf/sql-map-config.xml"/> <property name= "DataSource"

	DataSource "/>

</bean>


The value= "Web-inf/sql-map-config.xml" in the configuration parameter above is used to tell the Ibatis framework where the configuration parameter files it is to use are stored.

The property name= "DataSource" ref= "DataSource" in the above configuration parameters is used to tell the Ibatis framework that the data source to use is the database buffer pool that is configured in section 3.5.

3.7 How to use the autowired features provided by Spring 2.5.

Add the following row of configuration parameters to the spring parameter configuration file:

<context:annotation-config/>

3.8 How to automatically generate entity classes and parameter profiles, and so on, from a database table.

You need to use the ibator provided by the Ibatis official website, which can be installed in eclipse, and the method of installing Plug-ins in Eclipse is not described here.

After installing the Ibator plug-in, use Eclipse's new feature to add a parameter profile to the project that automatically generates the Ibatis entity class, in which you specify the JDBC data drivers on this computer and the parameters to connect to the database, and so on, configure the entity classes to be generated, DAO, The storage path of the SQL map file, if everything is OK, the configuration is completed automatically according to the table structure in the database to generate entity classes and DAO and so on.

How to apply 3.9 Ibator generated entity classes and DAO.

The first step is to add the generated SQL mapping file to the Ibatis global parameter configuration file, which is the parameter profile of the Ibatis framework. In section 3.6, the <property name= "configlocations" value= "Web-inf/sql-map-config.xml"/> which file is configured in this line, Then that file is the global configuration parameter file for the Ibatis framework.

The configuration parameter of the SQL mapping file that adds an entity class to the inside is the following format:

<sqlmap resource= "Edu/xw/ji/sqlmaps/t_db_history_sqlmap.xml"/>

3.10 How do you configure ibator automatically generated DAO classes in spring?

The implementation class of the DAO class that is automatically generated by the Ibator code generation tool is somewhat special, that is, the DAO implementation class needs to pass in a constructor parameter when instantiated. The data type of the class that is configured in section 3.6 is the parameter data type that the constructor will pass in, so it is OK to tell it what the constructor's parameters are when you configure the DAO instance in spring, as follows:

<bean id= "Sys_job_type_dao" class= "Edu.xw.ji.dao.SysJobTypeDAOImpl" >

	<constructor-arg>

		< Ref bean= "Sqlmapclient"/>

	</constructor-arg>

</bean>


3.11 How spring manages the Struts2 action class.

To make Struts2 's action class work with spring, you need to define the bean in spring, where the STRUTS2 action is defined as a normal bean in spring, The only place to note is to add a scope= "prototype" to the spring configuration parameters.

Then in the STRUTS2 parameter profile, use the bean ID that is defined in spring.

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.