SSH2 Integrated Development Steps

Source: Internet
Author: User

Three main frameworks for distribution integration:

The first step: assemble the three frameworks to develop the jar package and place the project in the lib directory, as shown in:


These jar packages are optional, but for a more comprehensive, thoughtful system, it is recommended to load these jar packages, and beginners are less prone to error.

Step Two: Create a new bean.xml under your own project ( this is mainly the three framework of the integration configuration )

① Configure the data source:

<!--used C3P0 data source configuration--><bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" Destroy-method= "Close" ><property name= "Driverclass" value= "Org.gjt.mm.mysql.Driver"/><property name= " Jdbcurl "value=" Jdbc:mysql://localhost:3306/itcast?useunicode=true&characterencoding=utf-8 "/>< Property name= "User" value= "root"/><property name= "password" value= "root"/><!--the number of connections obtained during initialization, The value should be between Minpoolsize and Maxpoolsize. Default:3--><property name= "Initialpoolsize" value= "1"/><!--the minimum number of connections that are kept in the connection pool. --><property name= "Minpoolsize" value= "1"/><!--the maximum number of connections that are kept in the connection pool. Default:15--><property name= "maxpoolsize" value= "/><!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. default:0--><property name= "MaxIdleTime" value= "/><!--when the connection in the connection pool is exhausted c3p0 the number of connections that are fetched at the same time. Default:3--><property name= "Acquireincrement" value= "5"/><!--every 60 seconds to check for idle connections in all connection pools. default:0--><property name= "Idleconnectiontestperiod" value= [/></bean>]

② the sssionfactory to Spring 's things manager and injects the data source into the sessionfactory :

<!--hibernate sessionfactory configuration--><bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/ > <property name= "mappingresources" >    <list>      <value>cn/jxau/po/employee.hbm.xml</ value>    </list></property> <property name= "hibernateproperties" > <value>      hibernate.dialect=org.hibernate.dialect.mysql5dialect< the dialect of the MYSQL5!--used--and      hibernate.hbm2ddl.auto= update<!--Whether the table structure is generated from the data based on the map source data, and      hibernate.show_sql=false<!--whether to print out the SQL statements in Hibernate  -- >      hibernate.format_sql=false<!--whether to format its SQL statement--  </value> </property></bean >


With this configuration, we get a single sesionfactory object for the database operation from Spring .

Step Three: Configure Spring 's things manager for hibernate

<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" ><property Name= "Sessionfactory" ref= "Sessionfactory"/></bean>

Manage the creation of sessionfactory objects.

Fourth step: Declaring things (which are configured here in the form of annotations)

<tx:annotation-driven transaction-manager= "Txmanager"/>

Fifth step: Configure the PO layer mapping file:

First build your own corresponding data table PO, and in the package corresponding to the corresponding XML file, such as PO 's name is Employee.java, The one that uses XML Mapping database tables, needs to build Employee.hbm.xml, This file is then given to the sessionfactory in Spring for management and is configured as follows:

<property name= "Mappingresources" >    <list>      <value>cn/itcast/po/employee.hbm.xml</ Value>    </list></property>

Sixth step: Write the Employee.hbm.xml file (database mapping configuration file):

<?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 ">

This completes the integration of hibernate and Spring , of course you can choose not to write the data table mapping file, using annotations to configure hibernate, This annotation configuration mapping I don't have to say much.

Sixth step: Integrate good hiberbate and spring, then test (test can be done with the following example, Personservice business Benan self-configuration, here only how to integrate the three framework):

The above is already integrated with hibernate and spring, but be aware that after writing the configuration, use the following code to test:

Package Test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.jxau.ruanjian1201.springweb1.po.person;import Com.jxau.ruanjian1201.springWeb1.service.impl.PersonService; Import Com.jxau.ruanjian1201.springwe1.service.personserviceimpl;public class Junittest {private static  Personserviceimpl personservice;public static void Setupbeforeclass () throws Exception {try {applicationcontext cxt = new Classpathxmlapplicationcontext ("Beans.xml");p Ersonservice = (Personserviceimpl) cxt.getbean ("PersonService");    System.out.println (Personservice);} catch (RuntimeException e) {e.printstacktrace ();}} public static void Main (string[] args) {try {setupbeforeclass ();p ersonservice.save (New person ("Linhao"));} catch ( Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Seventh step: After Spring and hibernate are integrated, they are integrated with STRUTS2

1. Struts2 and spring integration steps:

The first step: we need to instantiate the Spring container at the time of testing in order to use the things in spring, but in fact Spring has a listener on the outside, We just need to configure the Spring container listener on Web. XML . First instantiate the spring container in Web . xml

<!-- Specify the spring configuration file, the server starts by default from the Web root directory to find the configuration file, we can through the spring the supplied classpath prefix is specified from the Classpath .

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value> classpath:beans.xml</param-value>  </context-param>  <!--instantiate a spring container--  < Listener>  <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>  </listener>

The listener will spring Span style= "Font-family:times New Roman" >application If you want to get this instance, you can use the Span style= "Font-family:times New Roman" >application get

2, configuration struts2 jar package: Download good struts2 Compressed package after decompression, in its Lib All jar files without -plgin end in the directory , except Struts2-spring-plugin-2.0.11.1.jar (all the jar packages required above have been given)

3, the third step, using the filter in Web. xml to start struts2, configured as follows:

  <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>

4, configure struts2 configuration template struts.xml, I have listed here some configuration options, for the development of whether more helpful:

<?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> <!--Specifies the default encoding set for Web apps, which is equivalent to calling HttpServletRequest's Setcharacterencoding Method---& Lt;constant name= "struts.i18n.encoding" value= "UTF-8"/> <!--This property specifies the required Struts2 processing request suffix, which is the default value of the action, which is all matching *. Action requests are handled by STRUTS2 if the user needs to specify multiple request suffixes, the multiple suffixes are separated by commas (,) and <constant name= "Struts.action.extension" V Alue= "Do"/> <!--set whether the browser caches static content, the default value is True (used in production), and the development phase is best off---<constant name= "struts.serve.static.brows Ercache "value=" Flase "/> <!--when a struts profile is modified, the system automatically reloads the file from the default value of False (used in production), preferably open-to-<constant na during the development phase Me= "Struts.configuration.xml.reload" value= "true"/> <!--development environment to print out more detailed error messages--<constant name= "str Uts.devmode "value=" true "/><!--default View Theme--<constant name=" Struts.ui.theme "value=" simple "/> <!--struts2 and spring key entry points, this action is declared by spring to help create-<constant name= "Struts.objectfactory" value= " Spring "/> <package name=" person "namespace="/person "extends=" Struts-default "> <global-results> < Result>/web-inf/view/message.jsp</result> </global-results> <!--using wildcards to convert views--<action Name= "action_*" class= "personoperateaction" method= "{1}" ><!--the name of this class is the ID name of the bean that you configured in spring, Must be injected in beans.xml or with annotations since <result name= "list" >/WEB-INF/view/person.jsp</result> <result name= "Add" >/WEB-INF/view/person_add.jsp</result> <result name= "edit" >/WEB-INF/view/person_edit.jsp< /result> </action> </package></struts>
5. After completing the above steps, you can do SSH2 integrated development (the action in the control layer needs to be declared with the @controller (or configured in Beans.xml) to be managed with the spring things management mechanism for dependency injection).


SSH2 Integrated Development Steps

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.