The SSH Framework version is used in this article.
Struts Version-struts-2.3.12-all.zip.
Spring Version-spring-framework-3.0.1.RELEASE-A.zip
Hibernate Version-hibernate-3.2.5.ga.zip.
Http://struts.apache.org/download
Http://www.springsource.com/download/community
Http://sourceforge.net/projects/hibernate/files/hibernate3/
1. Add struts configurations
New Web project and import Struts-related jar packages
Ognl-3.0.6.jar
Struts2-core-2.3.12.jar
Xwork-core-2.3.12.jar
Commons-logging-api-1.1.jar
Commons-lang3-3.1.jar
Commons-fileupload-1.2.2.jar
Freemarker-2.3.19.jar
Commons-logging-1.1.1.jar
Commons-io-2.0.1.jar
Javassist-3.11.0.GA.jar (this jar package is available under Web-INF/lib in the struts2-blank-2.2.1.war sample project)
Note: If the jar package cannot be found, you can go to the Web-INF/lib in the struts2-blank-2.2.1.war sample project to copy the jarcopy in the past.
Create a class under the SRC package to inherit the actionsupport class. Write an action method and create struts. xml under the SRC package to configure the action.
Public class userlogin extends actionsupport {Public String login () {system. Out. println ("passed"); Return success ;}}
Struts. xml under classpath
<?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"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default"><action name="login" class="com.struts.action.UserLogin" method="login"><result name="success">/WEB-INF/pages/index.jsp</result></action> </package></struts>
Modify web. xml and add struts configurations
<! -- Struts jump action configuration start --> <filter-Name> struts </filter-Name> <filter-class> Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter </filter-class> <init-param> <param-Name> actionpackages </param-Name> <param-value> COM. * </param-value> </init-param> </filter> <filter-mapping> <filter-Name> struts </filter-Name> <URL-pattern> *. action </url-pattern> </filter-mapping> <! -- Struts jump action configuration end -->
Note: A web project with basic struts2 functions has been established so far. You can publish a project and access your own actions. Check whether there is any log output
2. Add spring configurations
Import jar package
Decompress the downloaded jarpackage to spring-framework-3.0.1.release-a.zip. Find all the jar packages in the spring-framework-3.0.1.RELEASE-A \ Dist directory and copy them to the Project lib directory.
Prepare applicationcontext. XML in classpath
<?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"> <bean id="UserLogin" class="com.struts.action.UserLogin"> </bean></beans>
Type spring configuration in Web. xml
<! -- Spring bean container configuration start --> <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> <! -- Spring bean container configuration end -->
Integrate spring and struts
Import struts to download the struts2-spring-plugin-2.3.12.jar package
Note: here we can write the class configuration in struts. XML
<Action name ="Login"Class ="Userlogin"Method ="Login"> Here, the class is associated with the bean configuration ID of spring.
So far, we have integrated two frameworks. Start the service and access the action. Discover or output logs. Normal.
Iii. Add hibernate configurations
Import jar package
Copy all the files in the hibernate3.jar and Lib folders under the hibernate decompression package to the project. Add your JDBC driver. (This depends on your database)
The code for creating the XML file hibernate. cfg. xml under classpath is as follows:
<? 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">
Create a applicationContext-hibernate.xml under classpath
Note: datasource in the following code is not used. You can also try another constructor of localsessionfactorybean. I don't want to talk about the API or source 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 "> <! -- Configure datasource start --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" driverclass "value =" oracle. JDBC. driver. oracledriver "/> <property name =" jdbcurl "value =" JDBC: oracle: thin :@ localhost: 1521: orcl "/> <property name =" user "value =" Weibo "/> <property name =" password "value =" Weibo "/> <property name =" minpoolsize "Value = "2"/> <property name = "maxpoolsize" valu E = "50"/> <property name = "initialpoolsize" value = "10"/> <property name = "maxidletime" value = "60"/> <property name =" acquireincrement "value =" 2 "/> </bean> <! -- Configure datasource end --> <! -- Configure sessionfactory start --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" configlocation "> <value> classpath:/hibernate. cfg. XML </value> </property> </bean> <! -- Configure sessionfactory end --> <! -- Configure hibernatetemplate --> <bean id = "hibernatetemplate" class = "org. springframework. orm. hibernate3.hibernatetemplate "> <property name =" sessionfactory "ref =" sessionfactory "> </property> </bean> </beans>
Injection with hibernatetemplate
<bean id="UserLogin" class="com.struts.action.UserLogin"> <property name="hibernateTemplate" ref="hibernateTemplate"></property></bean>
Modify userlogin class
Public class userlogin extends actionsupport {private hibernatetemplate; Public hibernatetemplate mask () {return hibernatetemplate;} public void mask (hibernatetemplate) {This. hibernatetemplate = hibernatetemplate;} Public String login () {student Stu = new student (); Stu. setname ("zhanglie"); hibernatetemplate. save (Stu); system. out. println ("passed"); Return success ;}
Ing file and pojo
package com.struts.model;public class Student {private String id;private String name;private Integer age;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Access the action you configured to complete the insert operation
Package and download the source code:
Http://pan.baidu.com/share/link? Consumer id = 403237 & UK = 1997312776