1. Import the corresponding package files required by struts, spring, and hibernate.
2. Web. xml configuration
<? 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> SSH </display-Name> <! -- Struts configuration --> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <filter-Name> Struts-cleanup </filter-Name> <filter-class> Org. apache. struts2.dispatcher. actioncontextcleanup </filter-class> </filter> <filter-Mapp Ing> <filter-Name> Struts-cleanup </filter-Name> <URL-pattern>/* </url-pattern> </filter-mapping> <! -- Struts integrates the configurations required by spring --> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> <listener-class> Org. springframework. web. util. introspectorcleanuplistener </listener-class> </listener> </Web-app>
3. Create a struts. xml configuration file in SRC.
<? 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> <! -- Struts2 delegate spring Management --> <constant name = "struts. objectfactory" value = "Spring"/> <! -- Struts2 package configuration --> <package name = "demo_action" extends = "struts-Default"> <action name = "userdetail" class = "userdetailbean" method = "get"> <result>/userdetail. JSP </result> </Action> </package> </struts>
4. Add the applicationcontext. xml file to the WEB-INF
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <! -- Configure the sessionfactory of hibernate in the form of a single example, and call the configuration file of hibernate in the form of classpath --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" configlocation "> <value> classpath: hibernate. cfg. XML </value> </property> </bean> <! -- Configure transactionmanager of hibernate in the form of a single example --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "> <ref bean =" sessionfactory "/> </property> </bean> <! -- Userdao implementer --> <bean id = "userdao" class = "Dao. hibernate. userdaohibernate "> <property name =" sessionfactory "> <ref bean =" sessionfactory "/> </property> </bean> <! -- ID and struts. the class configured in XML is the same. The class here is the real class --> <bean id = "userdetailbean" class = "action. useraction "Scope =" prototype "> <property name =" userdao "> <ref bean =" userdao "/> </property> </bean> </beans>
5. userdao class
Package Dao; import java. util. List; import entites. userinfo; public interface userdao {public abstract userinfo get (INT userid );}
6. userdao interface implementer
Package Dao. hibernate; import Java. util. list; import Org. springframework. orm. hibernate3.support. hibernatedaosupport; import Dao. userdao; import entites. userinfo; public class userdaohibernate extends hibernatedaosupport implements userdao {@ overridepublic userinfo get (INT userid) {return (userinfo) gethibernatetemplate (). get (userinfo. class, userid );}}
7. Struts action class useraction
Package action; import Java. util. date; import Java. util. list; import Java. util. map; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actionsupport; import Dao. userdao; import entites. userinfo; public class useraction extends actionsupport {private userdao; Public void setuserdao (userdao Dao) {userdao = Dao;} Public String get () {userinfo = userdao. get (40); setuserinfo (userinfo); return Super. success ;}}
8. View File userdetail. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
9. hibernate. cfg. xml file
false
COM. mySQL. JDBC. driver
111
JDBC: mysql: // localhost: 3306/javadb
root
Org. hibernate. dialect. mysqldialect
10. userinfo. HBM. xml file
<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <! -- Generated 2011-1-7 15:19:38 by hibernate tools 3.2.4.ga -->