Java Tutorial (v) SSH framework-Configuration

Source: Internet
Author: User

Preface: Starting from this blog I will continue to tell the Java Tutorial: SSH, the main content around the SSH framework analysis and construction, today, a brief introduction to SSH configuration.

the SSH configuration sequence is: spring-->hibernate-->struts

1. Build web-project and add Spring support, when adding jar packages note that packages such as the AOP Core Web commons are loaded, and then the next step is to Successfully added spring support;

2. Add hibernate Support, also note the choice of package,

Note In the next step, select Add to Spring (in Spring configuration file)

Next, select the existing applicationcontext.xml (existing spring configuration file)

After the next step, add the previously configured data source

After the next step, be careful not to create sessionfactory

Complete the addition of Hibernate support  

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "Driverclassname" Value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver" > </property> <property name= "url" value= "JDBC: Sqlserver://localhost:1433;databasename=sshdemo "> </property> <property name=" username "value=" test " ></property> <property name= "password" value= "123456" ></property> </bean> <bean id= " Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" DataSource "> <ref bean=" dataSource "/> </property> <property name=" Hibernateproperties "> &LT;PR ops> <prop key= "Hibernate.dialect" > Org.hibernate.dialect.SQLServerDialect </prop> &LT;/PROPS&G  T </property> <property name= "mappingresources" > <list> <value>com/ssh/vo/Person.hbm.xml< /value> </list> </property> </bean>

3. Generate the Vo class and the. hbm.xml file for Hibernate, as follows: after configuring the. hbm.xml file, be careful to check that the corresponding property corresponds to the field in the corresponding table:

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.xml

create 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 carefully

6. 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

Related Article

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.