Package com.ssh.vo; public class Person implements java.io.Serializable { //fields private Integer ID; private String name; private String password; Setter and Getter methods for corresponding attributes }.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 of the 2nd step of self-generated 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 whether the success. Test code such as the following:<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>
the test process may be wrong, most of the errors are due to your jar package version number is low or repeated import and other causes, so carefully check6. Join struts configuration: first join Sturts support to generate the Web. xml file. to configure the Web. xml file, configure for example 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. Join the form bean and action bean of struts (the form bean will be able to write normally). The action Bean is configured such as the following:
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 Struts-config.xml File: join Struts's plug-in
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" > <set-property property= " Contextconfiglocation " value="/web-inf/classes/applicationcontext.xml "/>
re-join:
<controller processorclass= "Org.springframework.web.struts.DelegatingRequestProcessor" > </ Controller>
The above two configurations are for the completion of 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. Instead, the value after name is the value of the path in <action> in Struts-config.xml <action-mapping> class= " Com.ssh.struts.action.PersonAction "> <property name=" Persondaoimpl "> <ref bean=" Persondao "/ > </property> </bean>
10. Add a test page to test.
OK, test success, configuration is complete!
Java Tutorial (v) SSH framework-Configuration