Struts + spring + hibernate

Source: Internet
Author: User
The combination of the three is perfect. The key to integration is the configuration file.
1. Web. XML is used to load filters, Servlets, and configuration files on the web server.
Struts is mounted here Org. apache. struts. action. actionservlet, as well as its configuration parameter config file struts-config.xml, spring is loaded here Org. springframework. web. context. contextloaderservlet also has its configuration file applicationcontext. XML, not listed for other omissions
Example:
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationcontext. xml </param-value>
</Context-param> <servlet-Name> action </servlet-Name>
<Servlet-class>
Org. Apache. Struts. Action. actionservlet
</Servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
 
<! -- Action Servlet Mapping -->
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
2. struts-config.xml this file
This is the strust proprietary configuration file, configuration form, Global Forwarding, action, message-resources (used to display prompt information), plug-in
The format is as follows:
<Struts-config>
<Form-beans>
<Form-bean name = "logonform" type = "com. binghe. Forms. logonform"/>
.....
</Form-beans>
<Global-forwards>
<Forward name = "Logon" Path = "/login. jsp"/>
.......
</Global-forwards>
<Action-mappings>
<Action Path = "/logon"
Type = "org. springframework. Web. Struts. delegatingactionproxy" name = "logonform"
Input = "/login. jsp" Scope = "request" Validate = "true">
<Forward name = "Logon" Path = "/login. jsp"/>
.....
</Action>
......
</Action-mappings>
<Controller locale = "true"/>
<Message-resources parameter = "com. binghe. Struts. applicationresources"/>
<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames"
Value = "/WEB-INF/validator-rules.xml,
/WEB-INF/validation. xml "/>
</Plug-in>
<Plug-in
Classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation"
Value = "/WEB-INF/applicationcontext. xml"/>
</Plug-in>
</Struts-config>
3. applicationcontext. xml
This is the proprietary configuration file of spring, which configures the proxy hibernate resource and Struts action.
The format is as follows:
<Beans>
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource"
Destroy-method = "close">
<Property name = "driverclassname">
<Value> com. Microsoft. JDBC. sqlserver. sqlserverdriver </value>
</Property> <property name = "url">
<Value> JDBC: Microsoft: sqlserver: // 127.0.0.1: 1400; databasename = books </value>
</Property>
<Property name = "username">
<Value> SA </value>
</Property>
<Property name = "password">
<Value> 123 </value>
</Property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
<Property name = "mappingresources">
<List>
<Value>
COM/binghe/hibernate/booktype. HBM. xml
</Value>
<Value>
COM/binghe/hibernate/book. HBM. xml
</Value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. sqlserverdialect
</Prop>
<Prop key = "hibernate. show_ SQL"> false </prop>
</Props>
</Property>
</Bean> <bean id = "transactionmanager"
Class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean> <bean id = "booktypedao"
Class = "com. binghe. Spring. booktype. booktypedaoimp">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
<Bean id = "bookdao"
Class = "com. binghe. Spring. Book. bookdaoimp">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
<Property name = "booktypedao">
<Ref bean = "booktypedaoproxy"/>
</Property>
</Bean>
<Bean id = "booktypedaoproxy"
Class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<Property name = "target">
<Ref local = "booktypedao"/>
</Property>
<Property name = "transactionattributes">
<Props>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "check *"> propagation_required, readonly </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
<Bean id = "bookdaoproxy"
Class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<Property name = "target">
<Ref local = "bookdao"/>
</Property>
<Property name = "transactionattributes">
<Props>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "zhuxiao *"> propagation_required </prop>
<Prop key = "check *"> propagation_required, readonly </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
<Bean name = "/booktype"
Class = "com. binghe. Struts. Action. booktype. booktypeaction"
Singleton = "false">
<Property name = "booktypedao">
<Ref bean = "booktypedaoproxy"/>
</Property>
</Bean>
<Bean name = "/book"
Class = "com. binghe. Struts. Action. Book. bookaction"
Singleton = "false">
<Property name = "bookdao">
<Ref bean = "bookdaoproxy"/>
</Property>
</Bean>
</Beans>
4. the Hibernate configuration file is pojoclassname. HBM. XML, pojoclassname is a JavaBean defined by you. You can put this configuration file in a directory with pojobean or another directory and then reference it in the following format:
<Hibernate-mapping package = "com. binghe. hibernate. Reader"> <class name = "Reader" table = "Reader" lazy = "false">
<ID name = "ID" column = "ID" type = "integer">
<Generator class = "native"/>
</ID> <property name = "code" column = "code" type = "string"
Not-null = "false"/>
<Property name = "name" column = "name" type = "string"
Not-null = "false"/>
<Property name = "userid" column = "userid" type = "integer"
Not-null = "false"/>
<Property name = "typeid" column = "typeid" type = "integer"
Not-null = "false"/>
<Role-to-one name = "user" class = "com. binghe. utils. userbean"
Insert = "false" update = "false">
<Column name = "userid"/>
</Role-to-one>
<Role-to-one name = "readertype"
Class = "com. binghe. hibernate. readertype. readertype" insert = "false"
Update = "false">
<Column name = "typeid"/>
</Role-to-one>
<Set name = "borrowbills" lazy = "false"
Inverse = "true" cascade = "NONE">
<Key column = "readerid"/>
<One-to-least
Class = "com. binghe. hibernate. borrowbill. borrowbill"/>
</Set>

</Hibernate-mapping>


All the configuration information above is extracted from a self-developed project.
Dependency injection/control inversion in spring is good. You can define any interface and then implement the methods in the interface to inject your interface to any place through the spring configuration file, the premise is that you must reference an interface. The interface definition and the getter method must be included in the reference. However, you can use this interface as an attribute similar to JavaBean, javaBean has getter and setter methods.
The transaction proxy in spring is quite good.
<Property name = "target">
<Ref local = "booktypedao"/>
</Property>
Target indicates the class to be injected and the interface implemented by the proxy class.
<Property name = "transactionattributes">
<Props>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "check *"> propagation_required, readonly </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
Transactionattributes is used to provide transactions for the methods of the proxy. For example, if you define a method starting with ADD, it can have transaction management. For all the operations in it, can implement the transaction mechanism. If there is an exception, the transaction will be rolled back.
The ORM ing mechanism of Hibernate is Orm, which is an object-oriented database query. The Query Class must be provided during query (for example, the book in the "from book" hql statement of the find method is not the table name but the class name ), to get its instance, the JavaBean attribute of the corresponding database must be of the object type, int and double must be defined as integer and double type, and the ing table should have a lazy attribute that should be set to false, otherwise, an error is returned when a record is loaded after the database is queried. One-to-multiple two-phase association:
One-to-multiple ing, complex
<Set name = "borrowbills" lazy = "false"
Inverse = "true" cascade = "NONE">
<Key column = "readerid"/>
<One-to-least
Class = "com. binghe. hibernate. borrowbill. borrowbill"/>
</Set>
The cascade attribute is not easy to use. If I set it to none when deleting a record, both parties must set it. Otherwise, an error is reported because mutual notification is not allowed.
Multi-to-one ing, simple
<Your-to-one name = "name" type = "com. classname" update = "false" insert = "false">
<Column name = "nameid"/>
</Role-to-one>

 

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.