Package com.ssh.vo; public class Person implements java.io.Serializable { //fields private Integer ID; private String name; private String password; Setter and getter method for the corresponding property }.hbm.xmlcreate a DAO class again
Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport; Import Com.ssh.vo.Person; public class Persondao extends Hibernatedaosupport {public void Insert (person per) { this.getsession (). Save ( per); This.getsession (). BeginTransaction (). commit (); } }
4. Configure the Applicationcontext.xml file again, you need to configure Hibernatetemplate support for Hibernatedaosupport, and Hibernatetemplate support is sessionfactory (this is the configuration in the 2nd step of the automatic generation of good):
<span style= "FONT-SIZE:18PX;" ><bean id= "hibernatetemplate" class= "Org.springframework.orm.hibernate3.HibernateTemplate" > <property name= "Sessionfactory" > <ref bean= "sessionfactory"/> </property> </bean> </span>
This allows us to integrate spring with hibernate.
5. Then inject the spring-configured hibernatetemplate into the previously defined Persondaoimpl:
<span style= "FONT-SIZE:18PX;" > <bean id= "Persondao" class= "Com.ssh.dao.PersonDAOImpl" > <property name= "Hibernatetemplate" > //This property has <ref bean= "hibernatetemplate"/> </property> </bean in Persondaoimpl ></span>
the above configuration can be very good just spring and hibernate, but we also need to test the success, the test code is as follows:<span style= "FONT-SIZE:18PX;" >public class Test {public static void main (string[] args) { ApplicationContext app = new Classpathxmlapplicationc Ontext ("Applicationcontext.xml"); Persondaoimpl PDI = (Persondaoimpl) app.getbean ("Persondao"); Person p = new person (); P.setname ("Jack"); P.setpassword ("123456"); Pdi.insert (P); }}</span>
errors may occur during the test, most of which are due to the low version of your jar package or duplicate import, so check carefully6. Add struts configuration: First add sturts support to build the Web. xml file. to configure the Web. xml file, configure the following:
<span style= "FONT-SIZE:18PX;" ><context-param> <param-name>contextConfigLocation</param-name> <param-value >/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> Org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1 </load-on-startup> </servlet></span>
7. Add the form bean and action bean for struts (the form bean is normal to write), andthe action Bean is configured as follows:
public class Personaction extends Dispatchaction { private persondaoimpl Persondaoimpl; Public Persondaoimpl Getpersondaoimpl () { return persondaoimpl; } public void Setpersondaoimpl (Persondaoimpl persondaoimpl) { This.persondaoimpl = Persondaoimpl; } Public Actionforward Insert (actionmapping mapping, actionform form, httpservletrequest request, HttpServletResponse response) { Personform personform = (personform) form;//TODO auto-generated method Stub Person per = new person (); Per.setname (Personform.getname ()); Per.setpassword (Personform.getpassword ()); Persondaoimpl.insert (per); return null; } }
8. Configure the Struts-config.xml file: Add struts plug-in
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" > <set-property property= " Contextconfiglocation " value="/web-inf/classes/applicationcontext.xml "/>
Add again:
<controller processorclass= "Org.springframework.web.struts.DelegatingRequestProcessor" > </ Controller>
The above two configurations are designed to complete spring and struts integration
9. Finally configure the Applicationcontext.xml file to inject spring into the action Bean :
<bean name= "/person" //Here is no longer an ID, but the value after name is the value of <action> path in <action-mapping> in Struts-config.xml class= "Com.ssh.struts.action.PersonAction" > <property name= "Persondaoimpl" > <ref bean= " Persondao "/> </property> </bean>
10. Add a test page to test.
OK, the test is successful, the configuration is complete!
Java Tutorial (v) SSH framework-Configuration