Integration training diary of struts, spring, Hibernate and other frameworks in December 1

Source: Internet
Author: User

Facade mode: it is the role of a daemon in Zhongguancun. It is responsible for interacting with multiple parts suppliers and assembling these parts for end customers.
Struts and spring are integrated in two ways:
The first is to call Spring applicationcontext to obtain the manager object in struts Action's execute method. Here the action itself does not use the spring idea, and the action cannot be configured as the JavaBean in spring, the Code is as follows:
Servletcontext application = This. getservlet (). getservletcontext ();
Webapplicationcontext wapp = (webapplicationcontext) webapplicationcontextutils. getwebapplicationcontext (application );
// Webapplicationcontext wapp = (webapplicationcontext) application. getattribute (webapplicationcontext. root_web_application_context_attribute );
Studentmanager = (studentmanager) wapp. getbean ("studentmanager ");
In addition, Spring provides a contextloaderplugin plug-in to integrate Struts. The function is to create an applicationcontext object and store the object in servletcontext, in the future, you can call getwebapplicationcontext in actionsupport to obtain this applicationcontext object, which does not make me feel much benefit. The sample code is as follows:
Enumeration E = application. getattributenames ();
While (E. hasmoreelements ())
{
String attributename = (string) E. nextelement ();
String objclassname = application. getattribute (attributename). getclass (). getname ();
Try
{
Response. getwriter (). println (attributename + ":" + objclassname + "<br> ");
} Catch (exception ex ){}
}
Studentmanager = This. getwebapplicationcontext (). getbean ("studentmanager "));

The second method is to configure struts action as a spring JavaBean. In the action, you only need to define a manager variable and the corresponding setter/getter method, you can inject a manager object to it through spring. This method is too complex and far-fetched. I personally think it has no practical significance. It is recommended that you do not have to spend time learning.

Principles of spring and hibernate integration:
Class mycontroller extend simpleformcontroller
{
Public mycontroller ()
{
Setcommandclass (studnet. Class );
}
}
First, review the steps for using hibernate:
1. Create and configure the configuration object. You can configure the congiuration object through hibernate. properties or the hiberate. cfg. xml file. You can also use the following programming method:
Configuration = new configuration ()
Configuration. addclass (student. Class)
//. AddFile ("student. HBM. xml ")
//. Addresource ("/student. HBM. xml ")
. Setproperty ("show_ SQL", "true ");
2. The sessionfactory object is created by Configuration:
Sessionfactory Sf = configuration. buildsessionfactory ();
3. Get the session object through sessionfactory and perform the CRUD operation:
Session session = SF. opensession ();
Session. Save ()/delete/update/load/
Session. Close ();
The first step to integrate hibernate with spring is to create the sessionfactory object with spring configuration.
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate. localsessionfactorybean">
<Property name = "mappingresource?>
<List>
<Value> XX. HBM. xml </value>
<Value> yyy. HBM. xml </value>
<List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "show_ SQL"> true </prop>
<Props>
</Property>
<Property name = "datasource"> <ref locale = ""/> </property>
</Bean>
In localsessionfactorybean, configuration objects are configured in full programming mode, instead of using the hibernate. properties or hiberate. cfg. xml configuration file. You can view the source code of localsessionfactorybean.
With sessionfactory, we can then inject this sessionfactory into the DAO class through spring. The configuration is as follows:
<Bean id = "studentdao" class = "cn. itcast. studentdao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
The calling program code is shown as follows:
Class studentdao
{
Sessionfactroy;
Void setsessionfactory (sessionfactory)
{
This. sessionfactory = sessionfactory;
}
Insert (User user)
{
Sessionfactory. opensession (); // tired
Session. Save (User );
Session. Close (); // tired
}
}
We also have our own opensession and getsession. Is this very tiring? To this end, Spring provides a configuration class hibernatetemplate, which can help us with opensession and closesession. This configuration class can be opensession. Obviously, it must be referenced by sessionfactory.
<Bean id = "hibernatetemplate" class = "hibernatetemplate">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
Following the IOC idea of spring, we need to inject the hibernatetemplate into the DAO class. The configuration is as follows:
<Bean id = "studentdao" class = "cn. itcast. studentdao">
<Property name = "hibernatetemplate">
<Ref bean = "hibernatetemplate"/>
</Property>
</Bean>

Class studentdao
{
Hibernatetemplate;
Void sethibernatetemplate (hibernatetemplate)
{
This. hibernatetemplate = hibernatetemplate;
}
Insert (User user)
{
Hibernatetemplate. Save (User); // isn't it nice that we stop using opensession and closesession?
}
}

In addition, spring also provides a hibernatesuport class, which can return a hibernatetemplate through the injected sessionfactory. Our DAO class inherits this hibernatesupport and can also obtain the hibernatetemplate object for crud operations, the configuration and schematic Code are as follows:
<Bean id = "studentdao" class = "cn. itcast. studentdao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
Class studentdao extends hibernatesuport
{
/* Hibernatetemplate;
Void gethibernatetemplate ()
{
Return hibernatetemplate;
}
Sessionfactroy;
Void setsessionfactory (sessionfactory)
{
This. sessionfactory = sessionfactory;
Hibernatetemplate = new hibernatetemplate (sessionfactory );
}*/
 
 
Insert (User user)
{

Gethibernatetemplate (). Save (User );
}
}

 

Finally, Wang zeyou asked me a detailed explanation of spring MVC registration binding:
Search for the editor in the spring help document and you can see information about the registration binding. customdateeditor is not automatically registered and needs to be called within the initbinder method. The sample code is as follows:
Protected void initbinder (
Httpservletrequest request,
Servletrequestdatabinder binder)
Throws servletexception {
Binder. registercustomeditor (date. Class, new customdateeditor (New simpledateformat ("yyyy/mm/DD"), true ));
}
A replacement method and the underlying implementation of registercustomeditor:
Call basecommandcontroller. setpropertyeditorregistrars (). Obviously, this can be configured as a controller attribute.
Class mypropertyeditorregistrar
{
Void registercustomeditors (propertyeditorregistry Registry)
{
Reguistry. registercustomeditor (byte []. Class, new bytearraymultipartfileeditor ());
}
}

About date data verification: by searching for validate on the validator help page of the struts help document, you can find the configuration help of datevalidator.

If dateeditor is registered and the data is successfully converted to a date field, then the following validator framework does not need to verify the Date Field, because validator verifies the string type fields that have been assembled into the bean, and the data has been successfully assembled into the date field, it is necessary to verify the data. The framework assembles the data into formbean, and validator verifies the data from formbean.

<Bean id = "mycontroller class =" cn. itcast. mycontroller ">
<Property key = "commandclass">
<Value> CN. itcast. Student </value>
</Prperty>
</Bean>
The configuration information above is equivalent to the following code, which involves the attribute binding issue. You need to convert the string "cn. itcast. Student" into a class object.
Class clazz = Class. forname ("cn. itcast. Student ");
Setcommandclass (clazz );

Note: You can use the resource attribute of the <import> element in the spring configuration file to import more configuration files to achieve information distribution configuration.

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.