17: 44
SSHThat isStruts + spring + hibernateFramework.
TheStruts2 + spring3 + hibernate3
1, CreateSSHEngineering.
2,AddHibernateAccess Database
Windows> show View> dbexplorer
● CreateDBConnection, this experienceMySQLAs a user experience database, other databases should be similar
●Right-click the configured connection-> OpenconnectionEstablish Database Connection.
● AddHibernateConfiguration:Two methods: Pojodao/springdaoThere is no big difference between the two, but it is slightly different during configuration.)
If you useSpring DaoYou must first referenceSpringSupportedLibrary
Right-clickProject:Myeclipse-> Add spring cap ....
By default
AddHibernateSupported: Project->Right-click-> Myeclipse-> Add hibernate cap...
NextIfPojoSelectHibernate. xfg. xml, IfSpringdaoSelectApplicatoncontext. xml
Select as neededXMLFile
Select database connection Configuration
Select an object class to generate a file storage path and Package
FinishCompleteHibernateConfiguration
AddSpring persistence JDBC LibrariesRight-click the project-> Properties-> JAVA
Build path-> myeclipse-> spring XXX persistence JDBC Libraries
● GenerateHibernatedaoEntity Data
Select database connectionSchma-> tableRight-click-> Hibernate Reverse EngineeringTo generateDao
IfPojodaoSelectBasicdao
IfSpringdaoSelectSpring Dao
NextInID
GeneratorSelectNativeSelect as neededOne-to-least/minus-to-oneAnd other options
Next-> finishIn the correspondingPackageThe correspondingXxxdao. Java, XXX. HBM. xmlAnd other files
● Add business layer Logic
Add the corresponding processing interface, which is used hereIowenerserviceInterface, addGetowners ()
Public interface iownerservice {
Public list getowners ();
}
AddOwenerserviceInherited fromIowenerserviceAnd then add the correspondingDaoObjectOwenerservice.
Then generateGetter/setter
Public class ownerservice
Implements iownerservice {
Public ownersdao getownerdao (){
Return ownerdao;
}
Public void
Setownerdao (ownersdao ownerdao ){
This. ownerdao = ownerdao;
}
Private ownersdao ownerdao; //Change as needed
Public list getowners (){
// Todo auto-generated Method
Stub
Return ownerdao. findall ();
}
}
● Add presentation layer Processing
Project->Right-click-> Add struts cap ....
SelectStruts2 Core/struts 2 spring(SlaveStrutsToSpringMust be selected)
FinishCompleteStrutsAdd
Add page and correspondingAction
●Action
Public
Class listaction extends actionsupport {
Public
Collection getowners (){
Return
Owners;
}
Public void
Setowenrsrv (iownerservice owenrsrv ){
This. owenrsrv = owenrsrv;
}
Private iownerservice owenrsrv;
Private
Collection owners;
/**
* @ Return
*/
Public
String execute (){
//
Todo auto-generated method stub
Owners
= This. owenrsrv. getowners ();
Return
Success;
}
}
●Index. jsp
<% @
Page Language = "Java" Import = "Java. util .*"
Pageencoding = "UTF-8" %>
<% @
Taglib prefix = "S" uri = "/Struts-tags" %>
<%
String
Path = request. getcontextpath ();
String
Basepath =
Request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype
HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base
Href = "<% = basepath %>">
<Title> my JSP 'index. jsp 'starting
Page </title>
<Meta
HTTP-equiv = "Pragma" content = "no-Cache">
<Meta
HTTP-equiv = "cache-control" content = "no-Cache">
<Meta
HTTP-equiv = "expires" content = "0">
<Meta
HTTP-equiv = "keywords"
Content = "keyword1, keyword2, keyword3">
<Meta
HTTP-equiv = "Description" content = "this is my page">
<! --
<Link
Rel = "stylesheet" type = "text/CSS"
Href = "styles.css">
-->
</Head>
<Body>
<P> <a href = "<s: URL
Action = 'listaction'/> "> List </a> </P> <br>
</Body>
</Html>
●List. jsp
<% @
Page Language = "Java" Import = "Java. util .*"
Pageencoding = "UTF-8" %>
<% @
Taglib prefix = "S" uri = "/Struts-tags" %>
<%
String
Path = request. getcontextpath ();
String
Basepath =
Request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype
HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base
Href = "<% = basepath %>">
<Title> my JSP 'list. jsp 'starting
Page </title>
<Meta
HTTP-equiv = "Pragma" content = "no-Cache">
<Meta
HTTP-equiv = "cache-control" content = "no-Cache">
<Meta
HTTP-equiv = "expires" content = "0">
<Meta
HTTP-equiv = "keywords"
Content = "keyword1, keyword2, keyword3">
<Meta
HTTP-equiv = "Description" content = "this is my page">
<! --
<Link
Rel = "stylesheet" type = "text/CSS"
Href = "styles.css">
-->
</Head>
<Body>
<Table>
<Tbody>
<S: iterator
Value = "items">
<Tr>
<TD>
<A> <s: Property
Value = "username"/> </a>
</TD>
<TD>
<A> <s: Property
Value = "descn"/> </a>
</TD>
</Tr>
</S: iterator>
</Tbody>
</Table>
</Body>
</Html>
●XMLConfiguration
Struts. xmlThe configuration is as follows:
<? XML
Version = "1.0" encoding = "UTF-8"?>
<! Doctype
Struts public "-// Apache Software Foundation // DTD struts Configuration
2.1 // en "" http://struts.apache.org/dtds/struts-2.1.dtd ">
<Struts>
<Constant
Name = "struts. Enable. dynamicmethodinvocation" value = "false"
/>
<Constant
Name = "struts. devmode" value = "false"/>
<Constant name = "struts. objectfactory"
Value = "Spring"/>
<Package
Name = "default" extends = "struts-Default">
<! --Listactionbean CorrespondsApplicationcontextOfBean-->
<Action
Name = "listaction" class = "listactionbean">
<Result>/list. jsp </result> </Action> </package> </struts>
Applicationcontext. xml
<? 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: P = "http://www.springframework.org/schema/p"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>
<Bean
Id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource">
<Property
Name = "driverclassname"
Value = "com. MySQL. JDBC. Driver">
</Property>
<Property
Name = "url"
Value = "JDBC: mysql: // localhost: 3306"> </property>
<Property
Name = "username" value = "root"> </property>
<Property
Name = "password" value = "sa"> </property>
</Bean>
<Bean
Id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property
Name = "datasource">
<Ref
Bean = "datasource"/>
</Property>
<Property
Name = "hibernateproperties">
<Props>
<Prop
Key = "hibernate. dialect">
Org. hibernate. dialect. mysqldialect
</Prop>
<Prop
Key = "hibernate. hbm2ddl. Auto"> Update </prop>
</Props>
</Property>
<Property
Name = "mappingresources">
<List>
<Value>./sshdemo/entities/owners. HBM. xml </value>
<Value>./sshdemo/entities/pets. HBM. xml </value>
<Value>./sshdemo/entities/types. HBM. xml </value>
<Value>./visits. HBM. xml </value>
</List>
</Property>
</Bean>
<Bean
Id = "ownersdao" class = "sshdemo. Entities. ownersdao">
<Property
Name = "sessionfactory">
<Ref
Bean = "sessionfactory"/>
</Property>
</Bean>
<Bean
Id = "petsdao" class = "sshdemo. Entities. petsdao">
<Property
Name = "sessionfactory">
<Ref
Bean = "sessionfactory"/>
</Property>
</Bean>
<Bean
Id = "typesdao" class = "sshdemo. Entities. typesdao">
<Property
Name = "sessionfactory">
<Ref
Bean = "sessionfactory"/>
</Property>
</Bean>
<Bean
Id = "visitsdao" class = "sshdemo. Entities. visitsdao">
<Property
Name = "sessionfactory">
<Ref
Bean = "sessionfactory"/>
</Property>
</Bean>
<! --
Dependency Injection
-->
<Bean
Id = "ownerserivce" class = "sshdemo. BLL. ownerservice">
<Property
Name = "ownerdao">
<Ref
Bean = "ownersdao"/>
</Property>
</Bean>
<Bean
Id = "listactionbean" class = "sshdemo. Actions. listaction">
<Property
Name = "owenrsrv">
<Ref
Bean = "ownerserivce"/>
</Property>
</Bean>
</Beans>
Web. xmlConfiguration
<? XML
Version = "1.0" encoding = "UTF-8"?>
<Web-app
Version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaeeHttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/classes/applicationcontext. xml </param-value>
</Context-param>
<Filter>
<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>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<Login-config>
<Auth-method> basic </auth-method>
</Login-config>
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>
</Web-app>
So farSSHFirst-time Architecture Experience.