Struts + spring + hibernate
This configuration environment: myeclipse5.5, Mysql5.0,Struts1.2,Spring2.0,Hibernate3.1
I,Construction
......
II, To use Struts , Spring , Hibernate Required packages must be imported
1 , Struts ( No difference from before )
2 , Spring
Import separately Spring 2.0 core Libraries , Spring 2.0 web Libraries
Select *. Jar copy To project /Web root/WEB-INF/lib ; Click Next
Select Applicationcontext. xml Directory, /Webroot/WEB-INF ; Click Finish
3 , Hibernate
Import HibernateOf course, you must first configure Datasource Let's stop talking about it.
Select Import Hibernate Select All
Check box: hibernate 3.1 core..., Hibernate 3.1 advanced..., spring 2.0 ORM/Dao .......
Select *. Jar copy To project /Web root/WEB-INF/lib ; Click Next
Here we chooseHibernateToSpringManage
Select the single-choice button spring configuration file ...... ClickNext
Select an existingApplicationcontext. xmlFile,
EnterSessionfactory ID:SessionfactoryClickNext
EnterBean ID:Datasource
SelectDB driver: SelectDatasourceClickNext
You do not need to createSessionfactory classClickFinish
Note: ViewApplicationcontext. xmlChanges
III,IngVo, Data Operations
First, the structure of the project is built, and the structure is relatively simple:
Org. chenwj. Dao
Org. chenwj. Form
Org. chenwj. Struts
Org. chenwj. Struts. Action
Org. chenwj. Vo
Ing tableUserinfoCreate a persistent classOrg. chenwj. VoDirectory
InDaoCreate a database operation classUserdaoHere we just insert the database,CodeAs follows:
PrivateSessionfactorySessionfactory;
PublicSessionfactory getsessionfactory (){
ReturnSessionfactory;
}
PublicVoidSetsessionfactory (sessionfactory ){
This.Sessionfactory= Sessionfactory;
}
/*User Registration*/
PublicBooleanRegist (userinfo user ){
Try{
SessionSession=Sessionfactory. Opensession ();
Transaction Tx = session. begintransaction ();
Session. Save (User );
TX. Commit ();
Session. Close ();
ReturnTrue;
}Catch(Exception ex ){
Ex. printstacktrace ();
ReturnFalse;
}
}
Use dependency injection,SetterSet ValueSessionfactory
This data layer has been completed
Iv. ConfigurationStruts-config.xml
AddAction,Form,JSPOmitted......
FirstStruts-config.xmlAdd a plug-in
<Plug-in
Classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-PropertyProperty="Contextconfiglocation"
Value="/WEB-INF/applicationcontext. xml"/>
</ Plug-in >
Why is this plug-in added?
Because Applicationcontext. xml Under Configuration Action , Let Action To Spring
Management to achieve Struts Dependency injection Mechanism
Next, add Cuntroller Here you can use Delegatingactionproxy Proxy
< Controller Processorclass =
"Org. springframework. Web. Struts. delegatingrequestprocessor" />
ControllerReplacedStrutsOfRequestprocessor, In the definitionAction, We can omit
TypeAttribute.(I personally prefer this)Next let's take a look at the configuredStruts-config.xml:
<Struts-config>
<Data-Sources/>
<Form-beans>
<Form-beanName="Userform"
Type="Org. chenwj. Struts. Form. userform"/>
</Form-beans>
<Global-Exceptions/>
<Global-forwards/>
<Action-mappings>
<ActionAttribute="Userform"Input="/Index. jsp"Name="Userform"
Path="/User"Scope="Request">
<ForwardName="Success"Path="/Success. jsp"/>
<ForwardName="Error"Path="/Index. jsp"/>
</Action> <! -- Type </span> <span style = "font-size: 10pt; color: teal; font-family:; "> do not write attributes </span> <SPAN lang =" En-us "style =" font-size: 10pt; color: teal; font-family: 'courier new '; "> -->
</Action-mappings>
<ControllerProcessorclass=
"Org. springframework. Web. Struts. delegatingrequestprocessor"/>
<Message-Resources
Parameter="Org. chenwj. Struts. applicationresources"/>
<Plug-in
Classname="Org. springframework. Web. Struts. contextloaderplugin">
<Set-PropertyProperty="Contextconfiglocation"
Value="/WEB-INF/applicationcontext. xml"/>
FONT-SIZ