SSH six Cores

Source: Internet
Author: User
<Strong> there are 6 steps in total (the source code is included at the bottom of the article, which can be used for reference after learning SSH) </strong> 1. Write a hibernate application to add users 1) user entity 2) userdao interface save (User U) 3) userdaoimpl implement userdao save (User U) {// use hibernate API} 2 let spring inject sessionfactory 1) import the jar package of spring in the previous application. In addition to the basic six packages, You have to import two jar packages Org. springframework. jdbc-3.0.5.RELEASE.jar Org. springframework. orm-3.0.5.RELEASE.jar Org. springframework. transaction-3.0.5.RELEASE.jar 2) add profile beans. XML <bean id = "sessionf Acloud "class =" org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" configlocation "value =" classpath: hibernate. cfg. XML "/> </bean> <bean id =" userdao "class =" package name. userdaoimpl "> <property name =" sessionfactory "ref =" sessionfactory "> </bean> 3) Obtain userdao in the test method, and then test save () userdao = (userdao) cxt. getbean ("userdao"); 3 Add the data source and inject it into sessionfactory 1) need to import 2 Package commons-dbcp.jar commo Ns-pool.jar Note: There are a lot of data source 3rd implementations, now we use one of them, commons-DBCP 2) Configure datasource <bean id = "mydatasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "value =" com. mySQL. JDBC. driver "/> <property name =" url "value =" JDBC: mysql: // localhost: 3306/spring_db "/> <property name =" username "value =" root "/> <property name =" password "value =" 123456 "/> </bean> 3) in sessionfa Datasource <bean id = "mysessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean"> <! -- Inject datasource Bean --> <property name = "datasource" ref = "mydatasource"/> <property name = "mappingresources"> <list> <value> COM/toceansoft/entity/ user. HBM. XML </value> </List> </property> <property name = "hibernateproperties"> <value> hibernate. dialect = org. hibernate. dialect. mysqldialect hibernate. hbm2ddl. auto = Update hibernate. show_ SQL = true </value> </property> </bean> 4) Test Result 4 Use the hibernatetemp provided by spring Late 1) add private hibernatetemplate in Dao; Public void setsessionfactory (sessionfactory) {This. hibernatetemplate = new hibernatetemplate (sessionfactory);} 2) Change the modification method to use hibernatetemplate save (o) hibernatetemplate. save (o); 5 use spring to cut into transactions at the business layer. 1) write an interface userservice public void add (User U) at the business layer. 2) write an implementation class userserviceimpl implementation interface userservice public void add (User U) {}3) Implementation class userserv Iceimpl provides the get/Set Method of userdao 4) in beans. configure the bean of userserviceimpl in XML and inject userdao 5) configure the Transaction Manager bean <bean id = "txmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> 6) Configure transaction notifications <TX: advice id = "txadvice" transaction-Manager = "txmanager"> <! -- Includes isolation level, transaction propagation attribute... --> <TX: Attributes> <TX: method name = "add *" propagation = "required"/> <TX: method Name = "*" propagation = "supports" Read-Only = "true"/> </TX: Attributes> </TX: Advice> 7) configure the AOP aspect <AOP: config> <AOP: pointcut id = "servicemethods" expression = "execution (* COM. toceansoft. service. impl. *. *(..)) "/> <AOP: Advisor advice-ref =" txadvice "pointcut-ref =" servicemethods "/> </AOP: config> 8) Test Result 9) throw an exception in the add method, and test the Observation Result throw new Runtimeexception (); 6 integrate Spring and Struts 1) convert the Java application above into a web application -- copy all the stuff under the SRC application above to the SRC 2 of the Web application) to import the basic jar package developed by struts2, you also need a special package struts-spring-plugin.jar to import the corresponding spring package Org. springframework. web. struts-3.0.5.RELEASE.jar Org. springframework. web-3.0.5.RELEASE.jar 3) Put Beans. XML is copied to the WEB-INF and renamed applicationcontext. XML (optional) 4) in the web. configure the listener <context-param> <param-Name> contextconfiglocation </para M-Name> <param-value>/WEB-INF/applicationcontext. XML, classpath *: applicationcontext2.xml </param-value> </context-param> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> 5) on the web. xml configuration of struts2 startup <filter> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter </filter-class> <! -- <Filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> --> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </URL -Pattern> </filter-mapping> 6) write useraction in the Web package, and provide the Set Method for the business layer userserivce 7) in appplicationcontext. configure useraction bean <bean id = "useraction" class = "com. toceansoft. web. action. useraction "Scope =" prototype "> <property name =" userservice "ref = "Userservice"/> </bean> Note: Scope = "prototype" 8) Configure useraction in struts. xml <! -- Jump to the add product page --> <action name = "user_toadd"> <result>/user_add.jsp </result> </Action> <! -- Add a product --> <! -- Key points: here, the class sets the bean ID of Action bean in the spring configuration file --> <action name = "user_add" class = "useraction" method = "add"> <result name = "success ">/grobal_message.jsp </result> </Action> 9) write 2 pages user_add.jsp grobal_message.jsp 10) deployment test

 

SSH six Cores

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.