SSH framework integration - lite version

 

Development environment:

(1)    OS:Windows XP SP3

(2)    DB:MySql 5.1.6

(3)    JDK:1.6.0_17

(4)    Server:Apache Tomcat 6.0.26

(5)    IDE:MyEclipse 5.5

 

SSH frame:

(1)    Struts2.1.8

(2)    Spring2.0.2

(3)    Hibernate3.2.5

Note: Due to the large number of Jar packages in the SSH framework, some Jar packages must be added selectively, and all of them will be abnormal, such as the Spring Jar package, and the Jar packages may also have version conflicts. During the entire integration process, the addition of the Jar package adopts the most streamlined configuration principle. For detailed configuration, please refer to the relevant instructions in the integration steps.

 

Integration steps:

1. Create a new Web Project

 

2. Add support for Struts2.1

(1) Add Jar package

basic

|__ freemarker-2.3.15.jar

|__ struts2-core-2.1.8.1.jar

|__ xwork-core-2.1.6.jar

|__ ognl-2.7.3.jar

|__ commons-io-1.3.2.jar

|__ commons-logging-1.0.4.jar

(2) Modify the configuration file WebRoot/WEB-INF/web.xml

<!-- Struts2.1 -->

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

(3) Configure src/struts.xml

<package name="example" extends="struts-default">

<action name="hello" class="com.tarena.action.HelloAction">

<result name="success">/jsp/hello.jsp</result>

</action>

</package>

(4) Add WebRoot/jsp/hello.jsp page

Any content

(5) Add com.tarena.action.HelloAction.java

public class HelloAction extends ActionSupport {

@Override

public String execute() throws Exception {

System.out.println("HelloAction:execute()...");

return "success";

}

}

(6) Deploy the ssh project

Deploy the application into Tomcat.

(7) Test hello.action

start tomcat

Test URL: http://localhost:8080/ssh/hello.action

(8)     

3. Add Spring2.0 support

(1) Add Jar package

dist

|__sping.jar

(2) Modify the WebRoot/WEB-INF/web.xml file

<!-- Spring 2.0 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

(3) Add WebRoot/WEB-INF/applicationContext.xml

The configuration information is empty

(4) Update the ssh project

异常:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

Supplement the Jar package in Spring

lib

|__ jakarta-commons

|__commons-logging.jar

      

(5) Restart Tomcat

If there is no abnormality, proceed to the next step.

(6) Test Spring's IOC

Spring's IOC can be tested by static testing, so that Spring can be tested without enabling the application server. The specific operation is as follows:

Copy the WebRoot/WEB-INF/applicationContext.xml file to com.tarena.config

Add content in com.tarena.config.applicationContext.xml

<bean id="helloService" class="com.tarena.service.HelloService">

<property name="str" value="hello"></property>

</bean>

Add com.tarena.service.HelloService

Test class com.tarena.test.TestHelloService

Note: JUnit4 is introduced for the convenience of testing.

If there is no abnormality in the test, proceed to the next step.

(7)     

 

4. Add support for Hibernate3.2

(1) Add the driver Jar package to access the MySql database

mysql-connector-java-5.1.6-bin.jar

(2) Add the Jar package of Hibernate3.2

hibernate3.jar

(3) Create entity classes and DAO interfaces

com.tarena.entity.User

Mapping file: com.tarena.entity.User.hbm.xml

com.tarena.dao.UserDao

(4) Apply IOC to realize DAO interface

Use HibernateTemplate to implement operations such as User addition, deletion, modification, and query.

(5) Test com.tarena.dao.UserDaoImpl

Test class: com.tarena.test.TestUserDao

异常:java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

Solution: add Jar package commons-dbcp.jar

lib

|__ jakarta-commons

|__ commons-dbcp.jar

异常:java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

Solution: add Jar package commons-pool.jar

lib

|__ jakarta-commons

|__ commons-pool.jar

 

The following exception solutions are the same as above, and all Jar packages are selected in Spring's lib directory.

java.lang.NoClassDefFoundError: org/dom4j/DocumentException

Solution: add Jar package dom4j.jar

java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap

Solution: add Jar package commons-collections.jar

java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter

Solution: Add the Jar package cglib.jar

java.lang.NoClassDefFoundError: antlr/ANTLRException

Solution: add Jar package antlr.ajr

(6) Test DAO results

 

5. User login registration instance

Please refer to the specific code for details.

Note:

(1) There is no problem in using JUnit to test Sping and Hibernate in the project, but when deploying the project to Tomcat, there will be a very strange exception when starting the application server:

java.lang.ClassNotFoundException: javax.transaction.TransactionManager

After checking the information on the Internet, I got the solution: add Spring's Jar package jta.jar

lib

|__j2ee

       |__jta.jar

 

(2) Since SSH is tested in the Tomcat application server, the necessary Struts2-integrated Spring package struts2-spring-plugin-2.1.8.1.jar must be added. Otherwise, when Tomcat starts normally, the form test will appear on the Login page abnormal:

Exception information: UserService in LoginAction is null

 

6. Epilogue

The simple integration of SSH has come to an end. If you use other features of SSH, you need to supplement the configuration of the SSH framework according to the exception information.

 

 

Note: Resource (source code + integrated document + Jar package) download link: http://download.csdn.net/source/2846694

Guess you like

Origin blog.csdn.net/yangfande362/article/details/6905962