Manual configuration three frame integration, spring+struts2+hibernate

Source: Internet
Author: User
Tags aop connection pooling constant manual i18n ssh tomcat server

Using the three frameworks, use MyEclipse to help us import the jar package will find some packages conflict or some of the packages are redundant, let us manually configure the three framework bar.

1, download the three main framework of the necessary jar package, download address: http://download.csdn.net/detail/harderxin/4420066

2, create a new Web project sshtest, import the jar package into the project, note that after importing into the Web-info directory in the Lib folder to have the corresponding jar package, the jar package as well as my directory structure


3. Open Web. XML, add the following code in it, and configure the following information to load the information in the spring and struts configuration files at the start of the project:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <listener> <listener-class> org.springframework.web.context.contextloaderlistener</listener-class> <!--default load:/WEB-INF/ Applicationcontext.xml-</listener> <context-param> <param-name>contextconfiglocation</ param-name> <!--loading Spring profiles: Applicationcontext-*.xml-<param-value>classpath: Applicationcontext-*.xml</param-value> </context-param> <!--Configure the STRUTS2 filter, which means that when you start a Web project, load-and Lt;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> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app >

4. Open Struts.xml, configure struts with some constants, and give struts to the spring container to manage

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/ Dtds/struts-2.0.dtd "> <struts> <!--give struts2 to spring management--<constant name=" Struts.configuration.xml.reload "value=" true "/> <constant name=" struts.action.extension "value=" Action,do, WebWork "/> <constant name=" struts.enable.DynamicMethodInvocation "value=" true "/> <constant name=" Stru
	Ts.devmode "value=" true "/> <constant name=" struts.multipart.maxSize "value=" 100971520 "></constant> <constant name= "struts.i18n.encoding" value= "UTF-8" ></constant> <constant name= " Struts.objectFactory.spring.autoWire "value=" name "></constant> <constant name=" Struts.objectfactory " Value= "Spring" ></constant> <constant name= "struts.custom.i18n.resources" value= "Messages" ></ constant> <package name= "test" namespace= "" extends= "Struts-default" > <action name= "testaction" class= "testaction" > <result name= "Test" >index.jsp<
 /result> </action> </package> </struts>

  5, open spring, configure connection pooling and transactions

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" XM Lns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/cont Ext "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx=" http://www.springframework. Org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://ww 
                     W.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www
                     . SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring -tx-3.0.xsd "> <context:annotation-config/> <context:component-scan base-package= "Com.ssh"/> <!--If you do not use a configuration file, you can use the  
     		Configure the data source <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" in the following ways > <property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "JDBC: Mysql://localhost:3306/lundui?useunicode=true&characterencoding=utf8 "/> <property name=" username "val ue= "root"/> <property name= "password" value= "123456"/> </bean>-&L t;! --Read the configuration file--<bean id= "Propertyconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Location "value=" Classpath:jdbc.properties "/> </bean> <!--database connection pool-<bean id=" DataSource "destroy-method=" Close "C lass= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" vaLue= "${jdbc_driver}"/> <property name= "url" value= "${jdbc_url}"/> <property name= "username" value= "$ {jdbc_user} "/> <property name=" password "value=" ${jdbc_password} "/> </bean> <bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <  
            Property Name= "DataSource" ref= "DataSource"/> <!--Single entity class configuration <property name= "Annotatedclasses" > <list> <value>com.ssh.common.model.User</value> <valu  
 		E>com.ssh.common.model.log</value> </list> </property>-- <property name= "Packagestoscan" > <list> <value>com.ssh.common.model</value> </ list> </property> <property name= "hibernateproperties" > <props> <prop key= "Hib Ernate.dialect ">org.hibernate.Dialect. mysqldialect</prop> <prop key= "Hibernate.show_sql" >true</prop> </props> </prope Rty> </bean> <!--configuration transaction Manager-<bean id= "Txmanager" class= "org.springframework.orm.hibernate3.h Ibernatetransactionmanager "> <property name=" sessionfactory "> <ref local=" sessionfactory "/> </ Property> </bean> <!--configuration Transaction propagation characteristics--<tx:advice id= "Txadvice" transaction-manager= "Txmanager" &G
 		T <tx:attributes> <tx:method name= "add*" propagation= "REQUIRED"/> <tx:method name= "del*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:method name= "save*" propagation= "R Equired "/> <tx:method name=" saveorupdate* "propagation=" REQUIRED "/> <tx:method name=" * "read-only=" Tru E "/> </tx:attributes> </tx:advice> <!--Configure which classes which methods use transactions--<aop:config> <aop:poin Tcut id= "ALlmanagermethod "expression=" Execution (* com.repair.*.service.* (..)) " /> <aop:advisor advice-ref= "txadvice" pointcut= "Execution (* com.repair.*.service). *.*(..))" /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Allmanagermethod"/> </aop:config> <!--jdbcte Mplate--<bean id= "JdbcTemplate" class= "org.springframework.jdbc.core.JdbcTemplate" > <property name= " DataSource "> <ref bean=" dataSource "/> </property> </bean> <bean id=" testaction "class=" C Om.ssh.test.action.TestAction "scope=" prototype "></bean> <bean id=" Testservice "class=" Com.ssh.test.service.impl.TestServiceImpl "></bean> <bean id=" Testdao "class=" Com.ssh.test.dao.impl.TestDaoImpl "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> &
 Lt;/beans>

Jdbc.propertie Database configuration file information:


6. Define XXX.hbm.xml file mappings for adding corresponding entity classes and classes

Package Com.ssh.common.model;

Import java.io.Serializable;
public class User implements Serializable {

	private static final long serialversionuid = -2699030436204817211l;
	
	Private Long ID;
	User name
	private String username;
	User Password
	private String password;
	Public Long getId () {
		return ID;
	}
	public void SetId (Long id) {
		this.id = ID;
	}
	Public String GetUserName () {
		return username;
	}
	public void Setusername (String username) {
		this.username = username;
	}
	Public String GetPassword () {
		return password;
	}
	public void SetPassword (String password) {
		this.password = password;
	}
}

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


7, in the above struts and spring, we give the corresponding entity class to register, the following to write our class, here, I use a three-tier architecture, we can be based on the truth of the corresponding configuration and processing, the method can refer to my configuration of the project package
8, deploy the project to the Tomcat server, if there is no error during the boot process, the three major SSH framework configuration OK. Let's give it a try.

I have configured a three-frame demo project on my own, so you can download it for free in my library:

Note: Use the database: MySQL, create a local database called Lundui, and then create a new Tb_user table inside, containing information: ID, username, password, or JDBC to configure itself

Demo Download Address: http://download.csdn.net/detail/harderxin/7054831

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.