Building the Struts2+spring+hibernate (SSH) framework

Source: Internet
Author: User

First need to download STRUTS2, spring,hibernate resource bundle;

STRUTS2 Resource Pack Download path: http://www.apache.org/
Spring Resource Pack Download path: http://projects.spring.io/spring-framework/
Hibernate resource Pack Download path: http://hibernate.org/orm/downloads/

Build steps in the SSH framework:

The first step: Create a Web project in Eclipse and generate the. xml file;

Step two: Import the jar package to the Lib directory:
1.struts2 Required jar Package: struts2.3.30\struts2-blank\webcontent\web-inf\lib (image)


2.spring jar Package Required: spring-framework-4.2.2.release-dist\spring-framework-4.2.2.release\libs (image)

3.hibernate Required JAR Package:

hibernate-release-5.2.2.final\lib\required(image)

Step three: Configure the Struts filter in the Web. xml file, spring's listener; (Code below)

<?xml version="1.0"encoding="UTF-8"? ><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation="Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"Id="webapp_id"version="3.1"> <display-name>ssh</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--configuring struts Filters--<filter> <filter-name>s Truts2</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> <!--Configure Spring Listener--<context-param> <param-name& Gt;contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value > </context-param> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener></web-app> 


Fourth step: Create four packages in SRC directory are Action control layer, DAO data layer, entity layer,
Service business layer; (picture description)

Fifth step: Struts.xml configuration: In the SRC Directory (code)

<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" >    <!--above the head, note the version, copy it from the sample Showcase.war\web-inf\src\java\struts.xml--><struts> <!--1th Step: Define a package first-- < PackageName= "mypck001"extends= "Struts-default" > <!--2nd step: Define an action, configure the jump information name is similar to the servlet @WebServlet ("/indexservlet") http://xxxx/project name/index.actionhttp://xxxx/Project name/index            classcorresponds to the action class you write when you do not write the method property, the default call is Execute--<action name= "Index"class= "Myindexaction" method= "execute1" > <!--Jump is forward/web-inf/is to prevent the JSP from accessing it without action--<result Name= "Success" >/WEB-INF/jsp/index2.jsp</result> <result name= "error" &G T;/web-inf/jsp/s_tag.jsp</result> </action> </ Package></struts>
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts    Public " -//apache software foundation//dtd Struts Configuration 2.3//en "    http://struts.apache.org/dtds/struts-2.3.dtd" ><!--above the head, note the version, is copied from struts2.3.3 version struts2-showcase.war\web-inf\src\java\struts.xml--><struts>    <!--tell Struts2 to use spring to create objects--    <constant name= "struts.objectfactory" value= "Spring"/>            <!--include files for segmentation, for multi-person concurrency--    <include file= "S001.xml"/>    <include file= "S002.xml"/ >    <include file= "S003.xml"/></struts>

Sixth step: Spring Configuration: Applicationcontext.xml file in src directory: (code)

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.2.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><!--introduce external properties files---<context:property-placeholder location= "classpath:jdbc.properties"/> <!-- Instances of a required class are managed by spring--<bean id= "Myindexaction"class= "Ssh.action.IndexAction" scope= "prototype" > <!--setis (myindexservice)--<property name= "is" ref= "Myindexservice"/> </bean> <!--Myindexservice =NewSsh.service.IndexServiceImpl ()--<bean id= "Myindexservice"class= "Ssh.service.IndexServiceImpl" scope= "prototype" > <property name= "id" ref= "Myindexdao"/> </bean> <bean id= "Myindexdao"class= "Ssh.dao.IndexDaoImpl" scope= "prototype" > <!--injected seesionfactory--<property name= "sessionfact Ory "ref=" mysessionfactory "></property> </bean> <!--the wrong way,NewOrg.hibernate.internal.SessionFactoryImpl () No, you need a configuration to create a bean ID= "Mysessionfactory"class= "Org.hibernate.internal.SessionFactoryImpl" ></Bean--<bean id= "Mysessionfactory"class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <!--inject connection pool, including database user name, password, etc.--< Property Name= "DataSource" ref= "myDataSource"/> <!--Configure other properties of Hibernate--<property Nam E= "Hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hibernate.dial Ect. mysql5dialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >true</prop> <prop key= "Hibernate.connection.autocommit" >false</prop> <!--boot auto-Generate table--<prop key= "Hibernate.hbm2ddl.auto" >update</pro            p> </props> </property> <property name= "Mappingresources" > <list> <value>ssh/entity/BookCard.hbm.xml</value> </list> </ property> </bean> <bean id= "myDataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" > <property name= "driverclass" value= "${j Dbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url} "/> <property name=" user "value=" ${jdb        C.user} "/> <property name=" password "value=" ${jdbc.password} "/> <!--check idle connections in all connection pools every 300 seconds-- <property name= "Idleconnectiontestperiod" value= "></property> <!--maximum idle time, the connection is discarded if not used in 900 seconds.        If 0, never discard-<property name= "MaxIdleTime" value= "></property>" <!--Maximum Connections--          <property name= "Maxpoolsize" value= "2" ></property> </bean> <!--dbcp Connection pool-- <!--switch DBCP connection pool to import Commons-dbcp.jar and Commons-pool.jar Package--<!--<bean id= "MyDataSource2" Destroy-meth Od= "Close"class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" value= "${jdbc.driver}"/> <property name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.user}"/> & Lt;property name= "Password" value= "${jdbc.password}"/> </bean>--></beans>

Building the Struts2+spring+hibernate (SSH) framework

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.