SSH2 Integration--Configuration + detailed

Source: Internet
Author: User

The latest project is done. , techniques used to organize and organize , and, incidentally, a systematic review of the Framework Foundation , To prevent learning without thinking is to be ignored . , thinking and not learning is dangerous .

let's talk about framework integration . . The initial architecture is the simplest Struts 2+spring3+hibernate3, The database uses a MySQL. list the requirements for consolidation, respectively. jar.

Struts:

Commons-fileupload.jarcommons-io.jarfreemarker.jarognl.jarstruts2-core.jarxwork-core.jarservlet-api.jar

Hibernate:

Hibernate3.jarrequired: Antlr.jarcommons-collections.jardom4j.jarjavassist.jarjta.jarslf4j-api.jarslf4j-log4j.jarmysql-connector-java-bin.jaropti Onal:c3p0.jarjpa:hibernate-jpa.jar

Spring:

Spring.jarcommons-logging.jar,log4j.jaraspectjjrt.jar,aspectjweaver.jar, Cglib-nodep.jarstruts2-spring-plugin.jar


The configuration and documentation of each part are

Struts.xml;

Hibernate.cfg.xml,*.hbm.xml,jdbc.properties,log4j.properties;

Applicationcontext.xml;

Because spring integrates hibernate, all configurations in the Hibernate.cfg.xml can be configured directly into the Applicationcontext.xml. This is a lot easier for spring enthusiasts like us. With these, You can configure it later in Web. Xml. Generally we always first integrate spring and hibernate, then integrate struts, not for anything else, easy to debug.

Application context.xml The contents are as follows :

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http Www.springframework.org/schema/tx/spring-tx-2.5.xsd Http://www.springframework.org/schema/jee/HTTP www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/contexthttp:// Www.springframework.org/schema/context/spring-context-2.5.xsd "><!--automatic scanning and assembly bean--><context: Component-scan base-package= "CoM.tgb.oa "></context:component-scan><!--Import External properties Files--<context:property-placeholder location= "Classpath:jdbc.properties"/><!--configuring Sessionfactory, handing the transaction in hibernate to Spring-managed <bean id= " Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!--configuration database connection pool, specifying Sessio Data source in Nfactory-<property name= "DataSource" > <bean class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--database connection information--<property name= "Jdbcurl" value= "${jdbcurl}" ></property> <property name= " Driverclass "value=" ${driverclass} "></property> <property name=" user "value=" ${user} "></property > <property name= "password" value= "${password}" ></property> <!--other Configurations-<!--get three connections when initializing The value should be between Minpoolsize and Maxpoolsize. Default:3--><property name= "Initialpoolsize" value= "3" ></property><!--the minimum number of connections that are kept in the connection pool. Default:3--><property name= "Minpoolsize" value= "3" ></property><!--the maximum number of connections left in the connection pool. Default:15--><property name= "Maxpoolsize" value= "5" ></property><!--when the connection in the connection pool is exhausted, c3p0 the number of connections fetched at the same time. Default:3--><property name= "Acquireincrement" value= "3" ></property><!-- Controls the number of preparedstatements loaded within the data source. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0--><property name= "maxstatements" value= "8" ></property><!-- Maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. default:0--><property name= "Maxstatementsperconnection" value= "5" ></property><!--maximum idle time, If not used within 1800 seconds, the connection is discarded. If 0, it will never be discarded. default:0--><property name= "maxidletime" value= "1800" ></property> </bean> </property&gt     ;                  <!--specify Hibernate properties--<property name= "Hibernateproperties" > <value>              <!--hibernate using dialects--hibernate.dialect=org.hibernate.dialect.oracle10gdialect <!--based on the reflection of the entityShoot file generation table structure-hibernate.hbm2ddl.auto=update <!--whether to print out SQL statements-- Hibernate.show_sql=false </value> </property> <!--hibernate mapping file--<prope Rty name= "Mappingresources" > <list> <value>com/tgb/oa/domain/User.hbm.xml</value> <value >com/tgb/oa/domain/Role.hbm.xml</value> </list> </property> </bean> <!--configuration using hibe based Rnate transaction Manager--<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" > <!--open transaction via sessionfactory-<property name= "sessionfactory" ref= "Sessionfactory" ></property&gt   ; </bean> <!--declarative transaction management (with annotations)--<tx:annotation-driven transaction-manager= "Txmanager"/></ Beans>


One of the files read here is jdbc.properties, when we want to modify the connection information, just modify the file.

Jdbcurl=jdbc:mysql:///projectoadriverclass=com.mysql.jdbc.driveruser=rootpassword=root

then the map part role . Java

Package com.tgb.oa.domain;/** * Post *  * @author ghy version 3.0.0, August 14, 2015 PM 10:22:59 */public class Role {private Long id;private string Name;private string description;public Long getId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;}}

Role . Hbm.xml The contents are as follows :

This is the mapping file, which configures the corresponding relationship between the entity and the database field.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


Next, specify the spring configuration file in Web. Xml.

The Web. Xml reads as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app id= "Webapp_9" version= "2.4" xmlns= "http://java.sun.com/xml/ Ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/ Java ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><!--Configure Spring's listener--><listener> < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationcontext*.xml</param-value></context-param><!--Configuration Struts2 Core Filter-< Filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher. ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filte R-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-fil E-list></web-app>


Now hibernate has been incorporated into Spring's management. Look at the following business code, the service layer bean has been managed by spring.

Roleservicebeanimpl . Java:

Package Com.tgb.oa.service.impl;import Java.util.list;import Javax.annotation.resource;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.tgb.oa.dao.roledao;import Com.tgb.oa.domain.role;import com.tgb.oa.service.roleservice;@ Service@transactionalpublic class Roleserviceimpl implements RoleService {@Resourceprivate Roledao roledao;@ Overridepublic list<role> findAll () {list<role> rolelist = Roledao.findall (); return rolelist;} @Overridepublic void Delete (Long id) {roledao.delete (id);} @Overridepublic void Save (role role) {roledao.save (role);} @Overridepublic role GetById (Long ID) {Role role = Roledao.getbyid (ID); return role;} @Overridepublic void Update (role role) {roledao.update (role);}}


@Service: This annotation is added to the business layer, which indicates that the class is managed by spring.

@Transactional: The class added by this annotation takes transaction management, and all methods in the class have transactional attributes. Add annotation attribute @transactional (propagation=propagation.not_supported) When a method does not require a transaction. The previous configuration of spring in Web. XML also adds the struts content. That is, the action is now managed by spring. The difference between action being managed by spring or not is that when you configure an action in Struts.xml, <action name= "role_*" class= "Roleaction" The >class attribute must specify the package name, with spring is not required to write.

The Roleaction.java code is as follows :

Package Com.tgb.oa.view.action;import Java.util.list;import Javax.annotation.resource;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.controller;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;import Com.opensymphony.xwork2.modeldriven;import Com.tgb.oa.domain.role;import com.tgb.oa.service.roleservice;/** * Job Management Action * * @author ghy version 3.0.0, August 17, 2015 PM 10:07:42 */@Controller @scope ("prototype") public class Roleaction Extends Actionsupport implements modeldriven<role> {@Resourceprivate RoleService roleservice;private Role model = New role (); @Overridepublic role Getmodel () {return model;} List public String list () throws Exception {list<role> rolelist = Roleservice.findall (); Actioncontext.getcontext (). Put ("Rolelist", rolelist); return "List";} Add page public String Addui () throws Exception {return "Saveui";} Add public String Add () throws Exception {Roleservice.save (model); return "TolisT ";} Modify Page Public String Editui () throws Exception {//Prepare echo data role role = Roleservice.getbyid (Model.getid ()); Actioncontext.getcontext (). Getvaluestack (). push (role); return "Saveui";} Modify public String edit () throws Exception {//Take the original object from the database role role = Roleservice.getbyid (Model.getid ());//Set the property to be modified role.se Tname (Model.getname ()); Role.setdescription (Model.getdescription ());//Update to Database roleservice.update (role); return " ToList ";}}

Configuration Action the Note @ scope, because struts 2 It's a lot of cases. , require each of the Action is a new object. , But when the action is given to spring management, the action is a singleton mode, which violates the Struts2 design concept,  so we need to declare the action as a prototype @scope ("prototype").


struts.xml content is as follows :


<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dt Ds/struts-2.0.dtd "><struts><!--configured for Development mode--<constant name=" Struts.devmode "value=" false "/> <!--Configure the extension to action--><constant name= "Struts.action.extension" value= "action"/><!--to configure the theme as simple-- ><constant name= "Struts.ui.theme" value= "simple"/> <package name= "Default" namespace= "/" extends= " Struts-default "> <!--when struts2 and spring are integrated, the class property can write the bean's name--<action name=" test "class=" Testac tion "> <result name=" Success ">/test.jsp</result> </action> &lt Action name= "role_*" class= "roleaction" method= "{1}" > <result name= "List" >/web-inf/jsp/roleaction/ list.jsp</result> <result name= "toList" type= "redirectaction" >role_list</result> <result name= "Saveui" >/web-inf/jsp/roleaction/saveui.jsp</result> </action> </package> </struts> 


Many projects are made by SSH, so the summary is still relatively handy. About the framework integration this matter, or to see the framework designer's meaning, the best way is to use the people to design good, direct and convenient.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SSH2 Integration--Configuration + detailed

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.