[Java EE] SSH Frame Note _eclipse building an SSH framework

Source: Internet
Author: User

The SSH framework is one of the most commonly used frameworks, and there are always people who encounter such problems when building an SSH framework. Let me introduce the whole process of SSH framework construction.
The first step: Prepare for work.
Download Good eclipse,struts2,spring,hibernate.
1.eclipse:eclipse download Java EE version of Eclipse is recommended when downloading.

Of course you can also download eclipse-sdk. (Download eclipse-sdk need to download Web,tomcat etc plugins)

2.struts2:http://struts.apache.org/download

1) Introduce struts's jar package. After downloading Struts-*-all.zip decompression, the Struts/lib directory is the relevant jar package for struts.
5 of them are required:
Commons-logging-1.0.4.jar,freemarker-2.3.13.jar,
Ognl-2.6.11.jar,struts2-core-2.1.6.jar,xwork-2.1.2.jar
The remaining jar packages are not required by struts. There are also 3 packages to be aware of the import. Exceptions may occur when you run Tomcat without importing.
Commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,javassist-3.7.ga.jar
Note: The Javassist-3.7.ga.jar package is under Web-inf/lib in the Struts2-blank-2.2.1.war sample project.

3.spring:http://www.springsource.com/download/community
You can also install downloads under Eclipse. The concrete steps are this:
1) Open Eclipse-help-software Updates.
2) In the Open dialog box, select the second item above (Available software).
3) Click the Add Site button to pop up the URL dialog box.
4) In the dialog box, enter: http://springide.org/updatesite/click OK.
5) Select Sping IDE Click Install.

4.hibernate:http://sourceforge.net/projects/hibernate/files/hibernate3/

5.JDK src.zip Package Import. (Of course not import also can ...) )

Step Two:
1. Create a Web progect and make a favorite name for yourself.
2. Modify the Web. xml file under Web-inf to increase the struts2 configuration.

XML code
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
Id= "webapp_id" version= "2.5" >
<display-name>SSHTest</display-name>
<!--struts Framework--
<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---
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3. Add the Struts.xml configuration file under the Web-inf/classes directory:
XML code
<?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>
<package namespace= "/" Name= "Struts2" extends= "Struts-default" >
<action name= "Login" method= "execute" class= "Loginaction" >
<result name= "Success" >/WEB-INF/jsp/login.jsp</result>
<result name= "Input" >/WEB-INF/index.jsp</result>
</action>
</package>
</struts>

4. Configure Spring

1) Import the spring package. After Spring-framework-**.zip decompression, the jar package in the Dist directory of the spring-framework-** folder is imported into the project.

2) Configure the Web. xml file.
XML code

<!--Spring Framework--
<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>

3) Add the Applicationcontext.xml file.
XML 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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!--Action--
<bean id= "Loginaction" scope= "prototype" class= "action. Loginaction "></bean>
</beans>

4) Integrate spring and struts. Find Struts2-spring-plugin-*.jar in the Lib directory of struts and introduce it into the project.

5. Configure Hibernate

1) Unzip the hibernate-distribution-*.zip. Import the jar packages in the hibernate-distribution-*ga/lib/required directory.

Hibernate3.jar Core Class Library
Antlr-2.7.6.jar code scanner for translating HQL statements
One of the Commons-collections-3.1.jar Apache Commons packages contains some of the Apache-developed collection classes,

Features stronger than java.util.*
Dom4j-1.6.1.jar a Java XML API, similar to Jdom, used to read and write XML files.
Javassist-3.4.ga.jar javassist byte code interpreter
The Jta-1.1.jar standard JTA API.
Slf4j-api-1.5.2.jar
Slf4j-nop-1.5.2.jar


2) Create Hibernate configuration file. The configuration file hibernate.cfg.xml for the linked database is established under the Web-inf/calsses directory.
(I am lazy, only access in the company's computer, too lazy to download other DBMS.) So the example is connected to access and everyone will see it.
* Note: You need to import Access_jdbc30.jar.
Hibernate.cfg.xml:
XML code

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Connection.driver_class" >
Com.hxtt.sql.access.AccessDriver
</property>
<property name= "Connection.url" >
Jdbc:access:///d:/workspace/sshtest/testdatabase.accdb
</property>
<!--database connection settings--
<property name= "Eclipse.connection.profile" >access</property>
<property name= "Connection.username" ></property>
<property name= "Connection.password" ></property>
<property name= "dialect" >com.hxtt.support.hibernate.HxttAccessDialect</property>
<!--show_sql Generate SQL statements--
<property name= "Show_sql" >true</property>
<!--SQL dialect dialect---
<property name= "Hibernate.dialect" >
Com.hxtt.support.hibernate.HxttAccessDialect
</property>
<!--Add a mapping file for an entity class--
<mapping resource= "Login.hbm.xml"/>

<!--annotation mode configuration
<mapping class= "entity. Login "/>
-
</session-factory>


Note: Using hibernate alone requires creating session factory class Hibernatesessionfactory.java
(If you use spring integration, you don't need it.) Spring is created in Applicationcontext.xml. )
Hibernat the operation of the database is achieved through the session, where the session is different from the page to pass the parameters of the session,
It is similar to Connection in JDBC. The session is the center of hibernate operations,
Object life cycle, transaction management, database access are closely related to the session.
The session is created by Hibernatesessionfactory and is thread-safe,
Allows multiple threads of execution to access hibernatesessionfactory at the same time without the problem of data sharing.
But you cannot have multiple threads sharing a session.

3) Login.hbm.xml File
XML code

<?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" >
<class name= "class name" table= "table name" >
<id name= "PRIMARY key in the Java class field name" Column= "in the corresponding table field" type= "type" >
<generator class= "PRIMARY key generation Policy"/>
</id>
</class>



6.Spring integrated Hibernate. Spring manages the entire lifecycle of Hibernate's session creation, submission, and closure.
1) Configure the sessionfactory and let spring create the session. Add the following code to the Applicationcontext.xml:
XML code

<!--sessionfactory--
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "Configlocation" >
<value>classpath:/hibernate.cfg.xml</value>
</property>
</bean>

[Java EE] SSH Frame Note _eclipse building an SSH framework

Related Article

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.