Java Tutorial (v) SSH framework-Configuration

Source: Internet
Author: User

starting from this blog, I will continue with the Java Tutorial: SSH. The main content around the SSH framework analysis and construction, today, first introduce the configuration of SSH.

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

1. Set up Web-project and join Spring support to Note When adding a jar package. The AOP Core Web Commons package is to be loaded. Then the next step is to successfully join spring support;

2. Join hibernate support. The choice of the same note 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. Join a previously configured data source

After the next step, be careful not to create sessionfactory

completed hibernate support Join  

<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. hbm.xml files for hibernate, such as the following: after configuring the. hbm.xml file, be careful to check that the corresponding properties are corresponding to the fields 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 methods for corresponding attributes     }.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 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 check

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

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.